Chapter I · The Language

Statements

A statement is a complete instruction or command that tells the JavaScript engine to perform an action.

01 What is a Statement?

Programs are built out of statements — syntactic units that command the engine to carry out specific actions, such as declaring variables, executing loops, or branching conditions.

Unlike expressions, which compute and return values, statements perform actions. They are often terminated with semicolons, though JavaScript's Automatic Semicolon Insertion (ASI) can handle missing punctuation in many common scenarios.

02 Simple Example

A variable declaration statement followed by an assignment command.

statement.js
let status = "active";
if (status === "active") {
  console.log("System is running.");
}

Each line or block command instructs the engine on how control flow should proceed.

03 How It Works

The engine parses statements sequentially to build the structural execution flow of your program.

if (true) { let msg = "hi"; console.log(msg); ✓ visible here } msg does not exist outside this block

Statements establish structural blocks and control commands across execution scopes.

04 Step-by-Step

  1. The engine encounters a statement declaration.

  2. It validates syntax rules against the ECMAScript specification.

  3. The command action is executed within the current execution context.

05 Mental Model

"A statement is a complete sentence or command in a recipe."

It tells the program precisely what action to take before moving on to the next instruction.

06 Common Mistakes

Relying on ASI in risky spots. Automatic Semicolon Insertion can silently change meaning — for example, a return followed by a newline and then a value on the next line gets treated as return; (returning undefined), because ASI inserts a semicolon right after return.

Best practice: write semicolons explicitly at the end of statements. It costs nothing and removes an entire category of ASI-related bugs, especially around lines starting with (, [, or backticks.

07 Interview Questions

Q: What's the difference between a statement and an expression?

A statement performs an action (like a for loop or an if block); an expression produces a value. Expressions can be part of statements, but not vice versa.

Q: Are semicolons required in JavaScript?

Not strictly — Automatic Semicolon Insertion inserts them for you in most cases — but omitting them intentionally requires knowing ASI's edge cases well, so most style guides recommend writing them explicitly.

Key Takeaway

Statements are the foundational commands that direct program execution flow and actions.

Related Concepts