javascript

Statements and Semicolons

Learn what statements are in JavaScript, how semicolons work, and what happens when you leave them out. Simple and beginner-friendly.

10 min read 7 sections Tutorial
Share

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.

javascript
1// 1. Declaration — create a variable or function
2let count = 0;
3const MAX = 100;
4function add(a, b) {
5 return a + b;
6}
7
8// 2. Expression Statement — do something with a value
9console.log('Hello!');
10count = count + 1;
11count++;
12
13// 3. Control Flow — decide what to run
14if (count > 0) {
15 console.log('Count is positive');
16} else {
17 console.log('Count is zero');
18}
19
20for (let i = 0; i < 3; i++) {
21 console.log('Loop:', i);
22}
23
24// 4. Block — group statements together
25{
26 let temp = 42; // only lives inside this block
27 console.log('temp is:', temp);
28}
29// console.log(temp); // ERROR — temp doesn't exist here
30
31console.log(add(5, 7)); // 12
StatementExampleEnds with ;?
Variablelet x = 5;Yes
Constantconst y = 10;Yes
Expressionconsole.log(x);Yes
Functionfunction foo() { }No
Classclass Foo { }No
if / elseif (x) { } else { }No
for loopfor (let i=0; i<3; i++) { }No
while loopwhile (x > 0) { }No
do...whiledo { } while (x > 0);Yes
returnreturn value;Yes
breakbreak;Yes
throwthrow 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.

javascript
1// ✅ With semicolons — clear and safe
2const a = 5;
3const b = 10;
4console.log('Sum:', a + b);
5
6// ✅ Without semicolons — also works in most cases
7const x = 5
8const y = 10
9console.log('Total:', x + y)
10
11// Multiple statements on one line (not recommended)
12let p = 1; let q = 2; console.log(p + q);
13
14// do...while ALWAYS needs a semicolon at the end
15let 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.

javascript
1// ASI adds ; after each of these lines — works fine
2let a = 1
3let b = 2
4console.log(a + b) // 3
5
6// Method chaining — ASI does NOT add ; here (correct!)
7// because the next line continues the expression
8const result = [10, 20, 30]
9 .find(x => x > 15)
10console.log(result) // 20
11
12// ASI always adds ; before a closing }
13function test() {
14 return 42 // ; added here by ASI
15}
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.

javascript
1// ── TRAP 1: return on its own line ───────────────────────
2// JavaScript adds ; right after return
3// So the function returns undefined — not the object!
4
5function getUser() {
6 return // ; added here by ASI ← BUG!
7 {
8 name: 'Alice'
9 }
10}
11console.log('WRONG:', getUser()) // undefined — not { name: 'Alice' }
12
13// FIX: put { on the same line as return
14function getUserFixed() {
15 return { // no ; added here — safe
16 name: 'Alice'
17 }
18}
19console.log('FIXED:', getUserFixed()) // { name: 'Alice' }
20
21// ── TRAP 2: Array on the next line ───────────────────────
22// JavaScript thinks the [ continues the line above it
23// So it reads: const x = 1[1,2,3].forEach(...) ← TypeError!
24
25// WRONG (uncomment to see the error):
26// const x = 1
27// [1, 2, 3].forEach(n => console.log(n))
28
29// FIX: add a semicolon
30const x = 1;
31[1, 2, 3].forEach(n => console.log('item:', n));
32
33// ── TRAP 3: Function call on the next line ────────────────
34// JavaScript might think () calls the result of the line above
35// WRONG (uncomment to see):
36// const fn = () => 42
37// (function() { console.log('hello') })()
38
39// FIX: add a semicolon after the first line
40const fn = () => 42;
41(function() { console.log('IIFE runs safely'); })();

Best Practices

Best Practices for Statements

javascript
1// ✅ One statement per line — always
2const firstName = 'Alice';
3const lastName = 'Smith';
4const fullName = firstName + ' ' + lastName;
5console.log(fullName);
6
7// ✅ Always use { } for if/else — even for one line
8const score = 85;
9
10// ❌ Bad — no braces, easy to break later
11// if (score >= 90) console.log('A');
12
13// ✅ Good — braces make it safe and clear
14if (score >= 90) {
15 console.log('A');
16} else if (score >= 80) {
17 console.log('B');
18} else {
19 console.log('C or below');
20}
21
22// ✅ Always put { on the same line as return
23function getConfig() {
24 return { // ← { here, not on the next line
25 theme: 'dark',
26 lang: 'en'
27 };
28}
29console.log(getConfig());
Pro Tips
  • 1Write one statement per line. Never put two statements on the same line.
  • 2Always use curly braces { } with if, for, and while — even if the body is just one line.
  • 3Always put the opening { on the same line as if, 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

Quick Check

What does this function return? js function getObject() { return { value: 42 } }

Quick Check

Which statement does NOT end with a semicolon?

Quick Check

What is ASI?

Quick Check

Which of these will cause a bug because of ASI?

Quick Check

What is an expression statement?

Quick Reference

Statements & Semicolons — Quick Reference

Pro Tips
  • 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: return on its own line, and lines starting with [ or (.
  • 5Always put { on the same line as return, if, for, etc. Never on the next line.
  • 6Always use { } braces for if/else and 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.
Continue Learning
🔡

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.

Continue Learning