JS Statements
In this page:
statement1;
statement2;
let variable = value;
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.
Example: What Is a Statement
// Declare the variable `score`, set to `10`
// Declare the variable `score`, set to `10`
let score = 10;
score = score + 5;
// Print `score` to the console
// Print `score` to the console
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.
Example: Semicolons
// Declare the variable `a`, set to `1`
// Declare the variable `a`, set to `1`
let a = 1;
// Declare the variable `b`, set to `2`
// Declare the variable `b`, set to `2`
let b = 2;
// Print `a + b` to the console
// Print `a + b` to the console
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.
Example: Code Blocks
// Check whether `true`
// Check whether `true`
if (true) {
// Declare the variable `message`, set to "Inside a code block"
// Declare the variable `message`, set to "Inside a code block"
let message = "Inside a code block";
// Print `message` to the console
// Print `message` to the console
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.
Example: Whitespace in 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.
Example: Line Length and Readability
const total =
10 +
20 +
30;
// Print `total` to the console
// Print `total` to the console
console.log(total);
- Relying entirely on automatic semicolon insertion in tricky situations, like a return statement followed by a new line, which can silently break code.
- Writing extremely long single-line statements that are hard to read, when breaking them across multiple lines would improve clarity.
- Forgetting that whitespace and indentation, while important for readability, have no effect on how JavaScript actually executes statements.
- 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.
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: