Lesson 5 +10 XP

JavaScript Statements

JavaScript Statements

A program is a list of statements that the computer executes in order.

What is a statement?

A statement is one instruction. It can do things like declare a variable, make a decision, or call a function:

let message = "Hello";   // statement 1
document.write(message); // statement 2

Statement keywords

Common JavaScript statement keywords:

  • let, var, const declare variables.
  • if, else, switch make decisions.
  • for, while, do create loops.
  • function and return define and return from functions.
  • break and continue control loops.
  • try, catch, throw handle errors.

Statement blocks

Statements can be grouped in blocks with curly braces { and }:

if (age > 18) {
  console.log("Adult");
}

Execution order

JavaScript runs statements top to bottom, one at a time. This is called sequential execution.

The semicolon rule

Each statement usually ends with a semicolon. The semicolon tells JavaScript where one statement ends and the next begins.

TL;DR

  • A statement is one instruction.
  • Keywords like if, for, and function start statements.
  • Blocks group statements with curly braces.
  • Statements run top to bottom.
  • End statements with semicolons.