TExcelTAD
Home › JavaScript › JS Statements

JavaScript Statements

A JavaScript program is a list of statements — instructions that the engine executes one after another, from top to bottom. Each statement does one thing: declare a variable, call a function, make a decision, repeat a block of code.

js · try it yourself
let apples = 5;                 // statement 1: declare a variable
let pears = 3;                  // statement 2
let fruit = apples + pears;     // statement 3: compute and store
console.log("Fruit:", fruit);   // statement 4: print
Fruit: 8

Semicolons

Statements are usually ended with a semicolon ;. Several statements may even share a line when they are separated by semicolons:

js · try it yourself
let a = 1; let b = 2; let c = a + b;
console.log(c);
3

JavaScript also has automatic semicolon insertion (ASI): if you leave a semicolon out at the end of a line, the engine usually adds one for you. Many style guides (and tools like Prettier) still write semicolons everywhere, because ASI has a few surprising corners:

js · try it yourself
function getStatus() {
  return
    "ready";
}
console.log(getStatus());
undefined

A line break directly after return ends the statement, so the function returns nothing (undefined) and the string on the next line is never reached. Keep the returned value on the same line as return.

Tip: Pick one style — always semicolons, or never — and let a formatter enforce it. This course uses semicolons.

White space and line length

Spaces, tabs and line breaks between tokens are ignored, so you can format code to be readable:

js
let total=price*quantity;          // valid but cramped
let total = price * quantity;      // same thing, easier to read

Keep lines reasonably short (about 80–100 characters). When a statement is long, break it after an operator or a comma:

js · try it yourself
const message =
  "JavaScript ignores extra white space, " +
  "so long expressions can span several lines.";
console.log(message);
JavaScript ignores extra white space, so long expressions can span several lines.

Code blocks

Curly braces { … } group statements into a block. Blocks belong to functions, if statements, loops and so on, and the statements inside run together:

js · try it yourself
const temperature = 31;
if (temperature > 30) {
  console.log("It's hot.");
  console.log("Drink some water.");
}
console.log("Done.");
It's hot.
Drink some water.
Done.

By convention, the code inside a block is indented by two (or four) spaces.

Keywords

Statements often start with a keyword — a reserved word that tells JavaScript what kind of action to perform. Keywords cannot be used as variable names.

Keyword Purpose
let, const, var Declare a variable
if, else, switch Choose between code paths
for, while, do Repeat code
break, continue Leave or skip a loop iteration
function, return Define a function and return its result
class, new Define and create objects
try, catch, throw Handle errors
import, export Share code between modules

Test yourself

In what order are JavaScript statements executed by default?

  1. Alphabetically
  2. Top to bottom
  3. Bottom to top
  4. In random order

What does a function return when return is followed by a line break and then a value?

  1. The value
  2. undefined
  3. A SyntaxError
  4. null

What groups several statements into a block?

  1. ( )
  2. [ ]
  3. { }
  4. < >