JavaScript Statements
A JavaScript program is a list of statements — instructions that the engine executes one after another, from top to bottom. Each statement does one thing: declare a variable, call a function, make a decision, repeat a block of code.
let apples = 5; // statement 1: declare a variable
let pears = 3; // statement 2
let fruit = apples + pears; // statement 3: compute and store
console.log("Fruit:", fruit); // statement 4: printFruit: 8
Semicolons
Statements are usually ended with a semicolon ;. Several statements may even share a line when they are separated by semicolons:
let a = 1; let b = 2; let c = a + b;
console.log(c);3
JavaScript also has automatic semicolon insertion (ASI): if you leave a semicolon out at the end of a line, the engine usually adds one for you. Many style guides (and tools like Prettier) still write semicolons everywhere, because ASI has a few surprising corners:
function getStatus() {
return
"ready";
}
console.log(getStatus());undefined
A line break directly after return ends the statement, so the function returns nothing (undefined) and the string on the next line is never reached. Keep the returned value on the same line as return.
Tip: Pick one style — always semicolons, or never — and let a formatter enforce it. This course uses semicolons.
White space and line length
Spaces, tabs and line breaks between tokens are ignored, so you can format code to be readable:
let total=price*quantity; // valid but cramped
let total = price * quantity; // same thing, easier to readKeep lines reasonably short (about 80–100 characters). When a statement is long, break it after an operator or a comma:
const message =
"JavaScript ignores extra white space, " +
"so long expressions can span several lines.";
console.log(message);JavaScript ignores extra white space, so long expressions can span several lines.
Code blocks
Curly braces { … } group statements into a block. Blocks belong to functions, if statements, loops and so on, and the statements inside run together:
const temperature = 31;
if (temperature > 30) {
console.log("It's hot.");
console.log("Drink some water.");
}
console.log("Done.");It's hot.
Drink some water.
Done.
By convention, the code inside a block is indented by two (or four) spaces.
Keywords
Statements often start with a keyword — a reserved word that tells JavaScript what kind of action to perform. Keywords cannot be used as variable names.
| Keyword | Purpose |
|---|---|
let, const, var |
Declare a variable |
if, else, switch |
Choose between code paths |
for, while, do |
Repeat code |
break, continue |
Leave or skip a loop iteration |
function, return |
Define a function and return its result |
class, new |
Define and create objects |
try, catch, throw |
Handle errors |
import, export |
Share code between modules |
Test yourself
In what order are JavaScript statements executed by default?
- Alphabetically
- Top to bottom
- Bottom to top
- In random order
What does a function return when return is followed by a line break and then a value?
- The value
- undefined
- A SyntaxError
- null
What groups several statements into a block?
- ( )
- [ ]
- { }
- < >