javascript interactive

JavaScript Interactive — a console crash course


2.4 Conditionals

Warning: Your browser may be outdated or unsupported.

The JavaScript console is known to display some of the examples incorrectly in Internet Explorer 8 or older.
>>>
if (true)
    print('yes');

  
The basic conditional if statement.
>>>
if (1 == 0) {
    print('yes');
} else {
    print('no');
}

  
The if-else conditional statement, also using blocks.
>>>
false ? 1 : 0
0
The conditional ?: operator will evaluate the value before ? to determine which of the values on either side of : to return. Note that due to operator precedence, the condition must often be parenthesized.
>>>
switch ('two') {
case 'one':
    print('1');
    break;
case 'two':
    print('2');
    break;
default:
    print('other');
}

  
The switch statement is sometimes used instead of long if-else constructs for clarity.
>>>

Index 1 2 3 4 5 6 7 8