JavaScript Statements

Code is Made of Statements

JavaScript Statements

A JavaScript program is a list of instructions called statements. Learn how they're written, separated, and grouped.

4 min read Level 1/5 #statements#semicolons#code-blocks
What you'll learn
  • Read a JavaScript program as a sequence of statements
  • Use semicolons and whitespace correctly
  • Group statements into a code block with `{ }`

A JavaScript program is a list of statements — instructions the browser runs one after another.

Three statements in order script.js
let x = 5;
let y = 10;
console.log(x + y);
▶ Preview: console

Each line above is one statement. They run from top to bottom, in order: declare x, declare y, then log their sum.

Semicolons

Statements end with a semicolon ;. JavaScript can insert most missing semicolons for you (this is called automatic semicolon insertion), but a few cases trip up the inserter — so writing them yourself avoids surprises.

Semicolons separate statements script.js
let name = "Ada"; let age = 36; console.log(name, age);
▶ Preview: console

You can put multiple statements on one line if you separate them with semicolons — but readability suffers. Use one statement per line.

Whitespace

JavaScript ignores extra spaces and line breaks inside a statement. You can format your code however you like.

Whitespace doesn't change behavior script.js
let total =   5
            + 10
            + 15;
console.log(total);
▶ Preview: console

Use whitespace and line breaks to make your code easier to read. There are no points for compact code.

Code Blocks

A group of statements wrapped in { } is called a block. Blocks are used everywhere — inside if, for, function, class, and so on — to say “these statements belong together”.

A code block script.js
{
  let message = "inside a block";
  console.log(message);
}
console.log("outside the block");
▶ Preview: console

Blocks also create scope — variables declared inside a block don’t leak out. We’ll come back to scope when we talk about variables.

Keywords

Some words have special meaning in JavaScript — like let, const, if, else, for, function, return, class. These are called keywords, and you can’t use them as names for your variables.

We’ll meet most of these one by one over the next lessons.

Up Next

You’ve seen statements. Now let’s look at the grammar inside them — values, operators, and identifiers.

JavaScript Syntax →