liveBook · Manning

Conditional logic

As explained in Pro Angular 16

An if/else statement conditionally selects one of two alternative blocks of code.

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

How Pro Angular 16 covers it

How deep it goes
Explains if/else structure, comparison pitfalls, and truthiness, but stops short of deeper control-flow internals.
Examples
Demonstrates name selection using if/else and contrasts equality with identity using numeric and string values.
What it assumes
Assumes basic programming-language familiarity and clarifies JavaScript-specific coercion and truthiness behavior.
What you can do after
Apply it via JavaScript or TypeScript if/else statements while avoiding unintended coercion and falsy-value behavior.
How it connects
Connects if/else with switch statements, equality operators, identity operators, truthiness, and logical operators.

Also explained in

Compare how 21 books explain conditional logic →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.