Chapter 14

Making Decisions with the If…Else Statement

When you need to make an either/or or yes/no decision, the if…else statement is your best friend. In this chapter, you learn how to use if…else statements to create and navigate choices in JavaScript programs.

image

Boolean Logic

In Chapter 9, we talk about JavaScript’s comparison operators and show you how they work in the JavaScript Super-Calculator. Let’s briefly review the comparison operators here.

Equality

The equality operators are == and ===. The difference between the two is that the double equals will convert values between different types. For example, if you compare 3 with "3", the == operator will say that they’re equal, while the triple equals operator will say that they aren’t.

The triple equals operator is called the strict equality operator. We recommend that you always use the strict equality operator in order to avoid bugs.

Not equal

The not equal operators are formed by putting an exclamation point (!) before the equality operators. They tell you whether two values are different. The strict not equal operator (!==) compares values without converting their type and the not equal operator (!=) converts between data types before comparing. We recommend that you always use the strict not equal operator.

Greater than and less than

The greater than (>) and less then (<) operators compare numeric values. ...

Get JavaScript For Kids For Dummies 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.