Console Learner — An interactive JavaScript course.


2.5 Conditionals

>>>
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 cond ? then : else conditional operator is sometimes a compact alternative to the if...else statement. Note that due to operator precedence (associativity), the condition or the whole expression 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.
>>>

The console allows you to interact with the course material and examples. Use the following keys:

A special logging function is also available:


Need more magic? Check out the other developer tools on this site.