π§Ύ JavaScript Statements vs Expressions β Clearly Explained with Examples

In JavaScript, understanding the difference between statements and expressions is crucialβespecially when writing clean, predictable code. While they often look similar, they serve very different purposes.
This article clears up the confusion with simple rules and clearly marked examples.
β What is an Expression?
Definition: An expression is any valid code that produces (emits) a value.
You can use it wherever a value is expected: in assignments, returns, function arguments, etc.
πΉ Expression Examples (π Emits a value):
5 + 3 // β
Expression β emits 8
'JS' + ' Rocks' // β
Expression β emits "JS Rocks"
true && false // β
Expression β emits false
42 // β
Expression β emits 42
x = 10 // β
Assignment expression β emits 10
Math.max(2, 9) // β
Function call expression β emits 9
'yes' || 'no' // β
Logical expression β emits "yes"
[1, 2, 3] // β
Array literal β emits an array
{ key: 'value' } // β
Object literal β emits an object
You can also capture the value like this:
let result = (x = 42); // x is 42, and result is also 42
π What is a Statement?
Definition: A statement is a complete instruction that performs an action.
It may include expressions, but it does not emit a value itself.
πΉ Statement Examples (βοΈ Performs action, no emitted value):
let x = 5; // β
Statement β declares a variable (not usable in expressions)
const name = 'JS'; // β
Statement β declaration, doesn't emit a value
if (x > 0) { // β
if β control statement
console.log(x); // β
statement inside if block
}
for (let i = 0; i < 3; i++) {
console.log(i); // β
statement inside for loop
}
function greet() { // β
Function declaration β statement
return 'Hello'; // β
return is a statement, "Hello" is an expression
}
Browser consoles show undefined
after statements like let x = 5;
because the statement doesnβt emit a value.
π Expression vs Statement in Context
β Invalid: Using a statement as an expression
// β This throws SyntaxError β statements can't be used where a value is expected
let result = (let y = 10);
β Valid: Using an expression where a value is expected
let result = (y = 10); // β
Assignment expression β result is 10
π§ Summary Table
Expression | Statement |
Emits a value (can be captured/returned) | Performs an action (no value emitted) |
Can go inside a statement | Can contain expressions |
Used in: assignments, returns, conditions | Used to control flow, declare variables |
Examples: 5 + 2 , x = 10 , "Hi" | Examples: let x = 10; , if , function |
π§ͺ Combined Example: Statement + Expression
// Full line is a statement
let status = x > 10 ? "High" : "Low";
// β
'let status = ...' β Statement
// β
'x > 10 ? "High" : "Low"' β Expression (ternary emits "High" or "Low")
π₯ Final One-Liner to Remember
Expressions emit values β you can assign, return, or pass them.
Statements perform actions β they control the program but donβt return anything themselves.
Subscribe to my newsletter
Read articles from pushpesh kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
