Conditional logic selects which code runs by testing an expression. An if/else statement chooses one block, can add else-if alternatives, and provides a final else branch when no condition matches. section 3.4.7
Selecting a name
let firstName = "Adam"; if (firstName == "Adam") { console.log("firstName is Adam"); } else if (firstName == "Jacqui") { console.log("firstName is Jacqui"); } else { console.log("firstName is neither Adam or Jacqui"); } The value "Adam" runs the first block. Replacing it with "Jacqui" runs the else-if block; any other value reaches else. section 3.4.7
Use comparisons carefully when conditions depend on JavaScript values:
- Use == when type coercion is intended; use === when both value and type must match. section 3.4.7
- Remember that false, 0, an empty string, null, undefined, and NaN are falsy; other values are truthy. section 3.4.7
Table 3.3. Useful JavaScript operators
|
Operator
|
Description
|
|
++,
--
|
Pre- or post-increment and decrement
|
|
+,
-,
*,
/,
%
|
Addition, subtraction, multiplication, division, remainder
|
|
<,
<=,
>,
>=
|
Less than, less than or equal to, more than, more than or equal to
|
|
==,
!=
|
Equality and inequality tests
|
|
===,
!==
|
Identity and nonidentity tests
|
|
&&,
||
|
Logical AND and OR
|
|
||,
??
|
Null and null-ish coalescing operators
|
|
?
|
Optional chaining operator
|
|
=
|
Assignment
|
|
+
|
String concatenation
|
|
?:
|
Three-operand conditional statement
|
Compare conditional branching with related JavaScript operators, including logical AND and OR and the three-operand conditional operator. section 3.4.7




















