Javascript Conditional Ternary Operator

If we simply have to test a variable and then do something based on whether this is true or do something else based on whether it is false then the Tenary Operator is perfect.

Heres a quick example of an if statement

We can rewrite this using the Tenary Operator as:-

As you can see the second one is much nicer, the value after the ? is what is assigned if the test is true, while the value after the : is assigned if the test is false.

In both examples speeding is given the value true

Please follow and like us:

Paul Brown

43 Year Old Web Developer

Leave a Reply

Your email address will not be published. Required fields are marked *