JS Statements
JavaScript Statements: Instructions That Drive Execution
A JavaScript statement is a single instruction that tells the engine to perform an action. Programs consist of statements executed in order from top to bottom, grouped inside curly-brace code blocks.
Statements vs. Expressions
Beginners often mix up statements and expressions, yet the difference is simple. An expression produces a value, while a statement performs an action such as declaring a variable or controlling flow.
For example, 25 * 2 is an expression because it evaluates to 50. Writing const price = 25 * 2; is a statement because it binds that result to price.
How Code Blocks and Semicolons Work
JavaScript groups related statements inside { ... } blocks for functions, loops, and if conditions. Semicolons mark the end of individual statements.
function calculateDiscount(subtotal) {
let rate = 0;
if (subtotal >= 100) {
rate = 0.15;
} else {
rate = 0.05;
}
const finalPrice = subtotal * (1 - rate);
return finalPrice;
}
console.log("Final price:", calculateDiscount(120));
Automatic Semicolon Insertion (ASI)
JavaScript engines insert missing semicolons at most line breaks through Automatic Semicolon Insertion. However, relying on ASI everywhere can trigger silent bugs when a line starts with (, [, or a template literal, or when return has a newline before its value.
Common Mistakes to Avoid
- Breaking lines after
return: Writingreturnon one line and the object{ ok: true }on the next line makes JavaScript returnundefined. - Starting a line with
[without a semicolon: Without a semicolon on the prior line, JavaScript may treat[1, 2].forEach(...)as property access on the previous expression. - Using inconsistent formatting: Let tools like Prettier enforce uniform semicolons and indentation across your project.
Frequently Asked Questions
Are semicolons required at the end of every JavaScript statement?
No, JavaScript does not require semicolons on every line because of Automatic Semicolon Insertion. Still, most engineering teams keep explicit semicolons to prevent edge-case parsing bugs.
Can I put multiple statements on a single line?
Yes, separating statements with semicolons allows multiple instructions on one line, such as let x = 1; let y = 2;. Keeping one statement per line is much easier to read and debug.
What happens to whitespace inside JavaScript statements?
JavaScript ignores extra spaces and tabs between tokens. Use consistent two-space indentation inside code blocks to keep nested logic clear.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.