JavaScript Syntax, Statements & Expressions
JavaScript Syntax, Statements & Expressions
The single biggest point of syntax confusion for beginners is the difference between an Expression (which creates a value) and a Statement (which performs an action). Mastering this distinction unlocks how JavaScript actually evaluates code.
The Golden Rule of JavaScript Grammar
Expressions
Any piece of code that evaluates to a single value. Like an English noun phrase or math formula.
5 + 5 → evaluates to 10"cat".toUpperCase() → "CAT"isMember ? 0.9 : 1.0 → discount rateMath.random() > 0.5 → true / falseStatements
A complete instruction that performs an action. Like a full English sentence ending in a period.
let score = 100; → Variable declarationif (score > 50) { win(); } → Control flowfor (let i = 0; i < 5; i++) {} → Loop iterationreturn true; → Function exit┌────────────────────────────────────────────────────────┐
│ JAVASCRIPT GRAMMAR │
└───────────────────────────┬────────────────────────────┘
│
┌───────────────┴───────────────┐
▼ ▼
┌───────────────────────┐ ┌───────────────────────┐
│ EXPRESSIONS │ │ STATEMENTS │
│ (Produces a Value) │ │ (Performs an Action) │
├───────────────────────┤ ├───────────────────────┤
│ • 2 + 2 │ │ • let x = 10; │
│ • user.name │ │ • if (user) { ... } │
│ • isActive ? 1 : 0 │ │ • for (let i of arr) │
│ • [1, 2, 3] │ │ • throw new Error(); │
│ │ │ • return value; │
│ Can sit on right side │ │ Controls flow, scopes,│
│ of assignment (=) │ │ or engine state │
└───────────────────────┘ └───────────────────────┘Side-by-Side: Common Grammar Patterns
Choose a scenario below to explore how statements and expressions differ in everyday JavaScript:
An if block cannot be assigned to a variable; a ternary expression produces a value directly.
// ❌ Statement: Cannot be assigned to a variable
let greeting;
if (hour < 12) {
greeting = "Good Morning";
} else {
greeting = "Hello";
}// ✅ Expression: Directly produces a value!
const greeting = hour < 12 ? "Good Morning" : "Hello";
// Can even be embedded directly inside template literals:
console.log(`User says: ${hour < 12 ? "Morning" : "Hello"}`);The Classic Trap: Automatic Semicolon Insertion (ASI)
JavaScript features Automatic Semicolon Insertion (ASI). If you break a line after return, break, continue, or throw, the engine inserts a semicolon automatically:
function getProfile() {
return // ⚠️ ASI inserts ';' here!
{
role: "admin"
};
}
console.log(getProfile()); // undefinedThe function exits on line 2 with undefined. The object below is never evaluated.
function getProfile() {
return { // ✅ '{' on same line prevents ASI!
role: "admin"
};
}
console.log(getProfile()); // { role: "admin" }Always place the opening brace { or parentheses ( on the same line as return.
Quick Knowledge Check
Test your understanding on statements vs expressions (one by one):
What is the core purpose of JavaScript Syntax, Statements & Expressions in JavaScript?
// Concept 4: JavaScript Syntax, Statements & Expressions
// Understanding the core mental modelSelect an answer: