Written by Chris Walker
The first part contains the conditional listed in parentheses. It is immediately followed by a question mark. This is because a ternary operator is a basic true/false statement. So, the question mark is the divider between the question and the results.
The second part is the code to be executed if the conditional turn out to be true. This is followed by a colon.
There are things to consider to using a ternary operator.
- Your web page will load faster: It uses less characters than an If/Else statement. Therefore, it will load your web site slightly faster. Ironically, the length can be exactly the same as a simple If statement.
- Less lines of code: Well, this is just the first one again. Ternary operators are generally written on one line. If/Else statements are usually written on several for readability.
Go To An Older Location
Go To A Newer Location
CSS Dropdown Menu Made Easy!