JavaScript statements are the building blocks of every program. These are instructions that the browser or JavaScript engine executes one after another. A program is essentially a list of these statements, executed in sequence – from top to bottom, exactly in the order they are written.
Each statement usually ends with a semicolon (;), which separates commands and prevents errors
in code interpretation. JavaScript is flexible regarding whitespace (spaces, tabs, newlines),
allowing developers to format code for maximum readability and clarity. To organize multiple statements into a
single logical unit, we use code blocks, marked with curly braces { ... }, which are
essential for functions, loops, and conditional statements.
Understanding the correct syntax of statements, the use of semicolons, and code formatting is fundamental to writing clean, efficient, and error-free code.
| Concept | Example | Description |
|---|---|---|
| Execution Order | ||
Sequential execution | let x = 5; let y = 10; |
Code executes from top to bottom, line by line. |
| Semicolon (;) | ||
Statement terminator | let a = 1; let b = 2; |
Semicolon marks the end of a statement. Allows multiple commands on one line. |
| Whitespace | ||
Indentation | if (true) { |
Spaces and tabs do not affect code execution but improve readability. |
| Line Breaks | ||
Line breaks | let longString = "This is a very long |
Long statements can be split into multiple lines for better readability. |
| Code Blocks { ... } | ||
Code blocks | { |
Curly braces group multiple statements together (e.g., in functions, loops, if statements). |
| Control Flow Statements | ||
if / else | if (x > 5) { ... } else { ... } |
Executes code if condition is true, otherwise executes alternative code. |
switch | switch(day) { case 1: ... break; } |
Selects one of many code blocks to be executed based on a value. |
| Loops & Jumps | ||
break | if (x === 5) break; |
Immediately terminates a loop or switch statement. |
continue | if (x % 2 === 0) continue; |
Skips the rest of the current iteration and moves to the next one. |
return | return x + y; |
Ends a function and returns a value. |