← Back to JavaScript Course | Chapter 1: Basics & Syntax | Lesson 4 of 12

JS Statements

Think of a recipe written as a list of steps, crack the eggs, mix the flour, heat the pan, each line is one complete instruction that the cook carries out in order before moving to the next. A JavaScript statement works the same way, it's one complete instruction that tells the browser to do something, like storing a value or displaying a message, and a full JavaScript program is simply a sequence of these statements running one after another. Understanding how statements are separated and grouped is the foundation for reading and writing any JavaScript code, whether it's a tiny script or a full application powering a site like cookiescursor.com.

What Is a Statement

A statement is a single instruction that the JavaScript engine executes, such as declaring a variable, calling a function, or assigning a new value. A JavaScript program is really just a sequence of statements run one after another, in order.

Note: Think of each statement as one complete thought, if a line does one clear thing, it's probably a single statement.

Warning: An expression alone, like just typing a number, is valid but does nothing useful on its own unless it's part of a full statement.

Example: What Is a Statement

javascript
let score = 10;
score = score + 5;
console.log(score);

Semicolons

Semicolons mark the end of a statement, and while JavaScript's automatic semicolon insertion can add them for you in many cases, writing them explicitly makes code more predictable and easier to read.

Note: Adopt the habit of ending every statement with a semicolon, it removes any ambiguity about where one instruction ends and the next begins.

Warning: Automatic semicolon insertion can misbehave around line breaks in certain patterns, such as after a return keyword, silently changing what your code does.

Example: Semicolons

javascript
let a = 1;
let b = 2;
console.log(a + b);

Code Blocks

Curly braces { } group multiple statements together into a single code block, commonly used to define the body of functions, loops, and conditional statements. Everything inside the braces is treated as one unit, even though it contains several individual statements.

Note: Consistent indentation inside code blocks makes nested logic dramatically easier to follow, even though JavaScript itself ignores that indentation.

Warning: A mismatched or missing closing brace is one of the most common syntax errors beginners encounter, always check that every opening brace has a matching close.

Example: Code Blocks

javascript
if (true) {
  let message = "Inside a code block";
  console.log(message);
}

Whitespace in JavaScript

JavaScript largely ignores extra spaces, tabs, and blank lines between statements, meaning code can be formatted for human readability without changing how it runs. This is why consistent indentation is a style choice enforced by convention and linters, not by the language itself.

Note: Use consistent spacing and blank lines to visually separate logical sections of your code, it costs nothing in performance and helps everyone reading it.

Warning: While extra whitespace is safe, removing whitespace that separates two distinct words or keywords, like writing constx instead of 'const x', breaks the code entirely.

Example: Whitespace in JavaScript

javascript
let      x    =    5;
console.log(x);

Line Length and Readability

There's no strict limit on how long a single JavaScript statement can be, but very long lines are hard to read and review, so breaking complex statements across multiple lines is a common best practice.

Note: When a statement gets long, breaking it after an operator or comma, with consistent indentation, keeps it readable without changing its behavior.

Warning: When manually breaking a statement across lines, make sure the break happens somewhere JavaScript won't insert an unwanted automatic semicolon.

Example: Line Length and Readability

javascript
const total =
  10 +
  20 +
  30;
console.log(total);
Common Mistakes
  1. Relying entirely on automatic semicolon insertion in tricky situations, like a return statement followed by a new line, which can silently break code.
  2. Writing extremely long single-line statements that are hard to read, when breaking them across multiple lines would improve clarity.
  3. Forgetting that whitespace and indentation, while important for readability, have no effect on how JavaScript actually executes statements.
Chapter Summary
  • A statement is one complete instruction, and a JavaScript program is a sequence of statements executed in order.
  • Semicolons mark the end of a statement, and while JavaScript can often infer them automatically, writing them explicitly avoids subtle bugs.
  • Curly braces group multiple statements into a single code block, commonly used with functions, loops, and conditionals.
Browser Support

Statement execution, semicolons, and code blocks are core JavaScript language features supported identically across every browser and JavaScript engine.

🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.