JavaScript programs consist of a sequence of statements executed by the engine in the order they are written. A statement performs an action, such as declaring variables, calling functions, evaluating conditions, or looping through data.
flowchart LR
Stmt["Statement: let total = price * quantity;"] --> Key["Keyword: let"]
Stmt --> Decl["Variable: total"]
Stmt --> Op["Assignment Operator: ="]
Stmt --> Expr["Expression: price * quantity"]
Stmt --> Term["Terminator: ;"]
| Concept | Definition | Example | Can Be Assigned to Variable? |
|---|---|---|---|
| Expression | Code unit that evaluates to a value. | 5 + 10, user.name, isReady ? 1 : 0 |
Yes |
| Statement | Action or instruction executing a control block or declaration. | if (x > 0) { ... }, for (...), let a = 5; |
No |
// Demonstrating statements, expressions, and block statements
// 1. Variable Declaration & Assignment Statements
let unitPrice = 49.99;
let quantity = 3;
let taxRate = 0.08;
// 2. Expression Statement evaluating total cost
let subtotal = unitPrice * quantity; // Expression: unitPrice * quantity
let grandTotal = subtotal + (subtotal * taxRate);
// 3. Conditional Control Statement Block
if (grandTotal > 100) {
// Block statement grouped by curly braces {}
let discount = grandTotal * 0.10;
grandTotal -= discount;
console.log(`Discount Applied! New Total: \$${grandTotal.toFixed(2)}`);
} else {
console.log(`Standard Total: \$${grandTotal.toFixed(2)}`);
}
// 4. Function Declaration Statement
function calculateShipping(weightKg) {
if (weightKg <= 0) return 0;
return weightKg * 2.50;
}
console.log(`Shipping Cost: \$${calculateShipping(4)}`);
[, (, or backticks.{}: Always use curly braces for if, for, and while blocks even if they contain only a single statement.Identify which line is an expression and which is a statement:
Line A: x = y + 10;
Line B: y + 10
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.