iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

JS Statements

A statement is one instruction the JavaScript engine executes. Multiple statements run top-to-bottom; semicolons separate them.

Kinds of statements

KindExample
Declarationlet x = 1;
Expression statementgreet("hi");
Control flowif, switch, for, while, return, break
Block{ … } — groups statements into one unit
EmptyA bare ; on its own

Semicolons — required or optional?

JavaScript uses Automatic Semicolon Insertion (ASI). Most lines work without a semicolon. But ASI has gotchas:

JS
// ❌ broken — JS inserts a ; after `return`
function bad() {
  return
    { value: 1 };   // → returns undefined
}

// ✓ same line
function ok() {
  return {
    value: 1
  };
}

// ❌ a line starting with [ or ( without a leading ; can join with the previous line
const a = 1
[1, 2].forEach(…)   // parsed as a[1, 2].forEach(…) → TypeError
Tip: Pick a style and run a linter (semi: always or semi: never). Mixed is the worst of both worlds.

Example

Example
<!DOCTYPE html>
<html>
<body>

<p id="out"></p>

<script>
document.getElementById("out").textContent = "Hello from JS Statements!";
</script>

</body>
</html>
Try it Yourself »

Exercise

Add the missing terminator so ASI does not bite.

const x = 1 [1, 2].forEach(fn);

Test yourself

Q1. ASI can break code starting with…
Q2. A block statement is wrapped in…
Q3. An empty statement is written as…

Discussion

Loading…