Conditionals

Conditionals let your code make decisions — running different code depending on whether something is true or false.

if, else if, else

Try changing the hour value below:

Try it yourself
Console output

JavaScript checks each condition in order and runs the first block whose condition is true. If none match, the else block runs.

The ternary operator

For a simple two-way choice, the ternary operator condenses an if/else into one line: condition ? valueIfTrue : valueIfFalse.

Try it yourself
Console output