JavaScript Statements

A JavaScript program is a list of programming statements. Statements are executed one by one, in the same order as they are written.

Statements and Semicolons

JavaScript statements are separated by semicolons. While not strictly required (ASI handles most cases), using them is best practice.

Example
let a = 5;
let b = 6;
let c = a + b;
console.log(c);   // 11

Code Blocks

Statements can be grouped together in code blocks, inside curly brackets {...}. Groups of statements are commonly found in functions.

Code Block in a Function
function calculate() {
    let x = 10;
    let y = 20;
    let sum = x + y;
    console.log("Sum:", sum);
}
calculate();

White Space

JavaScript ignores multiple spaces. Add spaces around operators for readability.

Readable Spacing
// These are equivalent:
let x=5+6;
let y = 5 + 6;   // much easier to read
console.log(x, y);

Common Keywords

KeywordDescription
varDeclares a function-scoped variable
letDeclares a block-scoped variable
constDeclares a block-scoped constant
ifExecutes a block conditionally
forMarks a loop block
whileMarks a loop block
functionDeclares a function
returnExits a function and returns a value
switchMarks a switch block
Exercise:
What are JavaScript statements typically separated by?
Try it YourselfCtrl+Enter to run
Click Run to see the output here.