Statements and comments
Overview
A JavaScript program is a list of statements — instructions the engine runs one after another, top to bottom. Each statement usually sits on its own line and ends with a semicolon.
const name = 'Asha';
const greeting = `Hello, ${name}`;
console.log(greeting);Before you learn variables, types and functions in detail, it helps to know how the engine reads your code: what counts as one instruction, how instructions are grouped, and which parts it ignores.
Statements and expressions
Two words come up constantly in programming, so it’s worth separating them early:
- An expression is any piece of code that produces a value:
2 + 3,'Hi',name,Math.max(1, 9). - A statement is a complete instruction that does something: declares a variable, makes a decision, repeats a loop.
Statements often contain expressions:
const total = 2 + 3; // a declaration statement; "2 + 3" is an expression
if (total > 4) { // an if statement; "total > 4" is an expression
console.log('Big!'); // an expression statement (a function call)
}This distinction matters later: some places accept only an expression (the inside of ${ } in a template literal, or the body of a short arrow function), while others expect statements.
Semicolons
A semicolon marks the end of a statement. JavaScript has a feature called automatic semicolon insertion (ASI) that adds missing semicolons for you, but its rules have traps.
A line that starts with ( or [ can be glued to the line before it:
const a = 1
const b = a
[1, 2].forEach((n) => console.log(n))
// Read as: const b = a[1, 2].forEach(...) → TypeErrorThe opposite problem happens after return. ASI does insert a semicolon after a bare return, so the value on the next line is never returned:
function getUser() {
return
{ name: 'Asha' };
}
getUser(); // undefined!Keep the value on the same line as return — or start the object on that line with return {.
Pick a style and stick to it. This course always writes semicolons, and a formatter such as Prettier can add them for you.
Whitespace and line breaks
Apart from those ASI rules, JavaScript ignores extra spaces, tabs and blank lines. These two snippets are identical to the engine:
const sum=1+2;
const sum = 1 + 2;Use whitespace for people: indent code inside blocks by two spaces, put spaces around operators, and leave blank lines between groups of related statements. JavaScript is also case-sensitive — total, Total and TOTAL are three different names.
Blocks
Curly braces group statements into a block. Blocks are used by if, loops and functions, and they also limit where let and const variables exist.
{
const message = 'inside the block';
console.log(message); // works
}
console.log(message); // ReferenceError: message is not definedBlocks can be nested, and each level is usually indented one step further, so you can see the structure at a glance:
if (isLoggedIn) {
for (const item of cart) {
console.log(item);
}
}No semicolon is needed after a block’s closing brace.
Comments
Comments are ignored by the engine. Use them to explain why code exists, not to repeat what it does.
// A single-line comment
/*
A comment that spans
several lines
*/
const tax = price * 0.2; // a comment can follow code on the same lineGood and bad comments
// Bad: repeats the code
i = i + 1; // add 1 to i
// Good: explains a decision a reader can't see
// The API counts pages from 1, not 0.
page = page + 1;Documentation comments
A comment that starts with /** is a JSDoc comment. Editors such as VS Code read it and show the description when you hover over the function or use it somewhere else:
/**
* Returns the price including 20% tax.
* @param {number} price - the price before tax
* @returns {number}
*/
function withTax(price) {
return price * 1.2;
}Commenting out code is a quick way to switch a line off while testing. In VS Code, select lines and press
Ctrl/Cmd+/. Delete commented-out code before you finish, though — that’s what version control is for.
Strict mode
Adding 'use strict'; at the top of a script turns some silent mistakes into errors. For example, assigning to a variable you never declared normally creates a global variable by accident; in strict mode it throws:
'use strict';
totl = 10; // ReferenceError: totl is not defined (a typo is caught!)Strict mode also forbids a few confusing old features, such as duplicate parameter names. It must be the very first statement in a file or function to take effect.
JavaScript modules (<script type="module"> and files that use import/export) and classes are always strict, so in modern code you often get strict mode without writing it.
Tidy up a script
Add a comment explaining what the script does, and put each statement on its own line.
let total = 0; total = total + 5; console.log(total);// Adds 5 to a running total and prints it
let total = 0;
total = total + 5;
console.log(total);Summary
- A program is a list of statements, run from top to bottom.
- An expression produces a value; a statement performs an action.
- End statements with semicolons — automatic insertion has surprising edge cases.
- Curly braces group statements into a block, which also limits
letandconst. - Use
//for single-line comments and/* */for longer notes. Explain *why*, not *what*.