Skip to content
Concept 04 // Module 01: JavaScript Foundations & Environment

JavaScript Syntax, Statements & Expressions

Module 01: JavaScript Foundations & Environment•Concept 04 of 100• 5 min read

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

The Value Makers

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 rate
• Math.random() > 0.5 → true / false
// Can be passed into function arguments or assigned with =
The Action Takers

Statements

A complete instruction that performs an action. Like a full English sentence ending in a period.

• let score = 100; → Variable declaration
• if (score > 50) { win(); } → Control flow
• for (let i = 0; i < 5; i++) {} → Loop iteration
• return true; → Function exit
// Cannot be passed as an argument or assigned to a variable
How The Parser Categorizes Code:
┌────────────────────────────────────────────────────────┐
│                   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 Approach (Action)
// ❌ Statement: Cannot be assigned to a variable
let greeting;
if (hour < 12) {
  greeting = "Good Morning";
} else {
  greeting = "Hello";
}
Expression Approach (Value)
// ✅ 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:

The Bug (Silent undefined)
function getProfile() {
  return // ⚠️ ASI inserts ';' here!
  {
    role: "admin"
  };
}

console.log(getProfile()); // undefined

The function exits on line 2 with undefined. The object below is never evaluated.

The Fix (Keep Opening Brace on Line)
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):

Score: 0 / 10
Question 1 of 10 • Core Purpose & Mental Model

What is the core purpose of JavaScript Syntax, Statements & Expressions in JavaScript?

// Concept 4: JavaScript Syntax, Statements & Expressions
// Understanding the core mental model

Select an answer: