Search⌘ K
AI Features

Be Careful Where You Break Lines

Explore how improper line breaks in JavaScript code can lead to unexpected behavior due to automatic semicolon insertion. Understand key rules of semicolon placement, see common pitfalls with examples, and learn best practices to write clearer, error-free JavaScript code by controlling line breaks thoughtfully.

Introduction

A number of dynamically typed programming languages don’t care about semicolons and treat them as optional syntax. JavaScript is not one of them. Semicolons are not optional. In some places, a semicolon is critical, while in other places, it’s more of a stylistic choice. However, if you leave out a semicolon where it’s required, JavaScript does not complain. That’s because JavaScript follows the philosophy that it’s more fun to take revenge than to complain.

📝Note: Merely placing a ; is not sufficient. We have to truly understand JavaScript’s automatic semicolon insertion (ASI) rules.

JavaScript’s automatic semicolon insertion (ASI) rules

The two main ...