Labs ICT
Pro Login

Semicolons

Semicolons — To Use or Not to Use

JavaScript has Automatic Semicolon Insertion (ASI), which means the interpreter adds semicolons for you in most cases. But ASI isn't magic — it follows specific rules and can lead to surprising bugs if you rely on it blindly.

The safest approach: always use semicolons. It's one extra keystroke that eliminates an entire class of bugs.

ASI in Action

let a = 5
let b = 10
console.log(a + b)
Try it Yourself →

This works because ASI inserts semicolons after each line.

Always Use Semicolons

let x = 10;
let y = 20;
console.log(x + y);
let result = x * y;
console.log(result);
Try it Yourself →

Mixed Style — Where ASI Fails

let value = 5
[1, 2, 3].forEach(n => console.log(n))

// Without semicolon, this is parsed as:
// let value = 5[1, 2, 3].forEach(...)
// Which throws an error

let value2 = 5;
[1, 2, 3].forEach(n => console.log(n));
Try it Yourself →

The first version fails because ASI doesn't insert a semicolon before [. Moral of the story: use semicolons.