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); // 11Code 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
| Keyword | Description |
|---|---|
| var | Declares a function-scoped variable |
| let | Declares a block-scoped variable |
| const | Declares a block-scoped constant |
| if | Executes a block conditionally |
| for | Marks a loop block |
| while | Marks a loop block |
| function | Declares a function |
| return | Exits a function and returns a value |
| switch | Marks a switch block |
Exercise:
What are JavaScript statements typically separated by?