Statements & Semicolons
Statements & Semicolons
A JavaScript program is made up of statements. A statement is one complete instruction — it tells JavaScript to do one thing.
Think of it like sentences in English. Each sentence ends with a full stop (.). In JavaScript, each statement usually ends with a semicolon (;).
Statement vs Expression — what's the difference?
An expression gives you a value: 5 + 3 gives 8, 'hello' gives a string.
A statement does something: let x = 5; saves a value, if (x > 0) { } checks a condition.
Expressions can go inside statements. Statements cannot go inside expressions.
Declaration
Creates a variable, function, or class. Example: let name = 'Alice';
Expression Statement
An expression used as a full statement. Example: console.log('hi');
Control Flow
Controls what runs and when. Example: if, for, while, return.
Block Statement
Groups many statements together inside { }. Used with if, for, functions.
Types of Statements
Types of Statements
JavaScript has several types of statements. Each one does a different job. Here are the most common ones with simple examples.
1// 1. Declaration — create a variable or function2let count = 0;3const MAX = 100;4function add(a, b) {5 return a + b;6}78// 2. Expression Statement — do something with a value9console.log('Hello!');10count = count + 1;11count++;1213// 3. Control Flow — decide what to run14if (count > 0) {15 console.log('Count is positive');16} else {17 console.log('Count is zero');18}1920for (let i = 0; i < 3; i++) {21 console.log('Loop:', i);22}2324// 4. Block — group statements together25{26 let temp = 42; // only lives inside this block27 console.log('temp is:', temp);28}29// console.log(temp); // ERROR — temp doesn't exist here3031console.log(add(5, 7)); // 12
| Statement | Example | Ends with ;? |
|---|---|---|
| Variable | let x = 5; | Yes |
| Constant | const y = 10; | Yes |
| Expression | console.log(x); | Yes |
| Function | function foo() { } | No |
| Class | class Foo { } | No |
| if / else | if (x) { } else { } | No |
| for loop | for (let i=0; i<3; i++) { } | No |
| while loop | while (x > 0) { } | No |
| do...while | do { } while (x > 0); | Yes |
| return | return value; | Yes |
| break | break; | Yes |
| throw | throw new Error('msg'); | Yes |
Semicolons
Semicolons
A semicolon ; marks the end of a statement. It tells JavaScript: "this instruction is done, start the next one."
In most cases, semicolons are optional — JavaScript can figure out where statements end on its own. But leaving them out in the wrong place causes bugs.
1// ✅ With semicolons — clear and safe2const a = 5;3const b = 10;4console.log('Sum:', a + b);56// ✅ Without semicolons — also works in most cases7const x = 58const y = 109console.log('Total:', x + y)1011// Multiple statements on one line (not recommended)12let p = 1; let q = 2; console.log(p + q);1314// do...while ALWAYS needs a semicolon at the end15let n = 0;16do {17 n++;18} while (n < 3);19console.log('n =', n); // 3
With or without semicolons — both are fine
Some developers always use semicolons. Others never use them. Both styles work. The only rule: be consistent. Use the same style in the whole project.
The easiest fix: install Prettier in VS Code. It automatically adds or removes semicolons for you every time you save. You never have to think about it.
Automatic Semicolon Insertion (ASI)
Automatic Semicolon Insertion (ASI)
ASI (Automatic Semicolon Insertion) means JavaScript adds semicolons for you in most places. It looks at your code line by line and decides: "Does this line end a statement? If yes, I'll add a semicolon here."
This is helpful — but ASI does not add semicolons everywhere. There are a few cases where it gets it wrong, and your code breaks in a surprising way.
1// ASI adds ; after each of these lines — works fine2let a = 13let b = 24console.log(a + b) // 356// Method chaining — ASI does NOT add ; here (correct!)7// because the next line continues the expression8const result = [10, 20, 30]9 .find(x => x > 15)10console.log(result) // 201112// ASI always adds ; before a closing }13function test() {14 return 42 // ; added here by ASI15}16console.log(test()) // 42
The Most Common ASI Traps
These are the most common mistakes developers make when they rely on ASI. Read them carefully — they are easy to make and hard to spot.
1// ── TRAP 1: return on its own line ───────────────────────2// JavaScript adds ; right after return3// So the function returns undefined — not the object!45function getUser() {6 return // ; added here by ASI ← BUG!7 {8 name: 'Alice'9 }10}11console.log('WRONG:', getUser()) // undefined — not { name: 'Alice' }1213// FIX: put { on the same line as return14function getUserFixed() {15 return { // no ; added here — safe16 name: 'Alice'17 }18}19console.log('FIXED:', getUserFixed()) // { name: 'Alice' }2021// ── TRAP 2: Array on the next line ───────────────────────22// JavaScript thinks the [ continues the line above it23// So it reads: const x = 1[1,2,3].forEach(...) ← TypeError!2425// WRONG (uncomment to see the error):26// const x = 127// [1, 2, 3].forEach(n => console.log(n))2829// FIX: add a semicolon30const x = 1;31[1, 2, 3].forEach(n => console.log('item:', n));3233// ── TRAP 3: Function call on the next line ────────────────34// JavaScript might think () calls the result of the line above35// WRONG (uncomment to see):36// const fn = () => 4237// (function() { console.log('hello') })()3839// FIX: add a semicolon after the first line40const fn = () => 42;41(function() { console.log('IIFE runs safely'); })();
Best Practices
Best Practices for Statements
1// ✅ One statement per line — always2const firstName = 'Alice';3const lastName = 'Smith';4const fullName = firstName + ' ' + lastName;5console.log(fullName);67// ✅ Always use { } for if/else — even for one line8const score = 85;910// ❌ Bad — no braces, easy to break later11// if (score >= 90) console.log('A');1213// ✅ Good — braces make it safe and clear14if (score >= 90) {15 console.log('A');16} else if (score >= 80) {17 console.log('B');18} else {19 console.log('C or below');20}2122// ✅ Always put { on the same line as return23function getConfig() {24 return { // ← { here, not on the next line25 theme: 'dark',26 lang: 'en'27 };28}29console.log(getConfig());
- 1Write one statement per line. Never put two statements on the same line.
- 2Always use curly braces
{ }withif,for, andwhile— even if the body is just one line. - 3Always put the opening
{on the same line asif,for,return, etc. Never on the next line. - 4Use Prettier to auto-format your code. It handles semicolons and braces automatically on every save.
Use Prettier — stop worrying about semicolons
Install Prettier in VS Code. Set semi: true or semi: false in .prettierrc. From now on, Prettier adds or removes semicolons automatically every time you save. It also fixes the return trap and other ASI problems. You never have to think about this again.
Test Your Knowledge
Test Your Knowledge
What does this function return?
js function getObject() { return { value: 42 } }
Which statement does NOT end with a semicolon?
What is ASI?
Which of these will cause a bug because of ASI?
What is an expression statement?
Quick Reference
Statements & Semicolons — Quick Reference
- 1A statement is one complete instruction. JavaScript runs them one by one, top to bottom.
- 2An expression gives you a value. When you use an expression as a statement, it is called an expression statement.
- 3A semicolon
;marks the end of a statement. It is optional in most places, but skipping it in the wrong place causes bugs. - 4ASI automatically adds semicolons — but not everywhere. Know the traps:
returnon its own line, and lines starting with[or(. - 5Always put
{on the same line asreturn,if,for, etc. Never on the next line. - 6Always use
{ }braces forif/elseand loops — even for just one line. - 7Write one statement per line. Multiple statements on one line are hard to read.
- 8Use Prettier — it handles semicolons and formatting automatically.
Case Sensitivity
How uppercase and lowercase affect your variable names, keywords, and built-in functions.
Comments
How to write notes in your code that JavaScript ignores.
Variables
let, const, and var — how to store and use values in your program.
Control Flow
if/else, switch, and loops — how to control what your program does.
You understand statements and semicolons!
You now know what statements are, how semicolons work, what ASI does, and the common traps to avoid. Keep it simple: one statement per line, always use braces, and let Prettier handle the rest.
Try it in the Javascript Compiler
Run and experiment with Javascript code right in your browser — no setup needed.