JS Statements
A statement is one instruction the JavaScript engine executes. Multiple statements run top-to-bottom; semicolons separate them.
Kinds of statements
| Kind | Example |
|---|---|
| Declaration | let x = 1; |
| Expression statement | greet("hi"); |
| Control flow | if, switch, for, while, return, break |
| Block | { … } — groups statements into one unit |
| Empty | A 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);
A single character.
Discussion
Loading…