CHAPTER 6

image

Conditionals

Conditional statements are used to execute different code blocks based on different conditions.

If Statement

The if statement only executes if the expression inside the parentheses is evaluated to true. In JavaScript, this does not have to be a Boolean expression. It can be any expression, in which case zero, null, NaN, empty strings, and undefined variables are evaluated as false, and all other values are true.

if (x < 1) {  document.write("x < 1");}

To test for other conditions, the if statement can be extended by any number of else if clauses. Each additional condition will only be tested if the preceding conditions ...

Get JavaScript Quick Syntax Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.