Skip to chapter content
navan✳THE BUILD LABJAVASCRIPT
0/31 read
CHAPTER 05 / 31FLOW AND SCOPE

How a program is read.

Expressions produce values; statements guide what happens.

01 / THE BASICS

Start here.

A JavaScript file is read as a sequence of instructions. An expression is code that produces a value: 3 * 4 produces 12, and score >= 50 produces true or false. A statement tells the program to do something: const score = 72; creates a binding, while an if statement chooses whether to run a block. An expression can appear inside a statement. The right side of an assignment is one common place where that happens.

Braces group several statements into a block. In if (score >= 50) { console.log('Well done'); }, JavaScript first evaluates the expression inside the parentheses. If it is true, the block runs; otherwise it is skipped. The >= operator means greater than or equal to. The string in quotation marks is text to display. Indentation is for readers: the braces, not the spaces at the start of the line, define the block.

The conditional expression condition ? valueIfTrue : valueIfFalse chooses between two values. It fits when the result is a simple value you want to assign, such as a label. An if block fits when the branches perform several steps. To read unfamiliar code, first find the statements that govern order, then evaluate the expressions inside them. This separates the question 'what value is produced?' from 'what happens next?'

02 / THE IDEA

Build the model.

An expression evaluates to a value. A statement performs a step such as declaring a binding, returning from a function, or choosing a branch. This distinction helps you read unfamiliar syntax: look for the parts that produce data and the parts that move control.

An if statement chooses which block runs; a conditional expression chooses one of two values. Neither form is automatically better. Use the form that makes the decision's purpose easiest to see, especially when the branches do more than produce a single value.

03 / UNDER THE SURFACE

How it works.

Read a line from the inside out. An expression such as score >= 50 yields a boolean. A conditional expression uses that boolean to choose one of two values. A declaration then stores the chosen value in a binding. Separating these jobs makes dense syntax less mysterious: each piece either produces a value or directs what executes next.

Statements such as if, return, and for shape the route through a program. Use a conditional expression for a small choice that must produce a value. Use if when branches have multiple actions, need comments, or need early returns. A statement may contain expressions, and an expression can contain other expressions; being able to name each layer helps you debug one layer at a time.

04 / A WORKED EXAMPLE

Read it line by line.

JAVASCRIPT / Worked example
const score = 72;
const label = score >= 50 ? 'pass' : 'retry';
if (score >= 50) {
  console.log('Well done');
}

First notice. The first decision yields a label. The second decides whether an action runs.

Assume score is 72. The comparison score >= 50 evaluates to true. The conditional expression therefore chooses 'pass', and label receives that string. The separate if evaluates the same comparison again and calls celebrate. In a complete program, score and celebrate would need to be defined before these lines run.

05 / ANOTHER EXAMPLE

See the rule again.

JAVASCRIPT / Another example
const temperature = 18;
const coat = temperature < 15 ? 'yes' : 'no';
console.log(coat);

The output is 'no'. The comparison is false, so the value after the colon is selected. An if version would start with let coat; then assign 'yes' in the true branch and 'no' in the else branch. The conditional expression is shorter because both branches only choose a value.

06 / KEEP THIS

One thing to remember.

Read control flow separately from the values flowing through it.

Finished this chapter?
Built with curiosity. By Navan Sami. ↗JavaScript · Understand the language behind the interface.