What Is an Expression Statement?
At its core, an expression statement is an expression used in a position where JavaScript expects a statement. The critical characteristic of an expression statement is that the expression is evaluated, but its result is subsequently discarded. This seemingly simple concept has profound implications for how we write JavaScript code.
When we write expression statements, we're essentially telling JavaScript to "do this thing" without caring about the result value. This makes expression statements ideal for operations that produce side effects rather than return meaningful values. Understanding how JavaScript expressions interact with statements is foundational to writing clean, predictable code--especially when integrating JavaScript with CSS in modern web applications.
1expression;2 3// Examples:4console.log("Hello World"); // Function call expression statement5counter += 1; // Assignment expression statement6index++; // Increment expression statement7delete user.oldData; // Delete expression statementThe Fundamental Distinction: Expressions vs. Statements
To truly understand expression statements, we must first understand the distinction between expressions and statements in JavaScript.
Expressions Produce Values
An expression is any valid unit of code that resolves to a value. Expressions can be simple or complex, and they can contain other expressions nested within them:
// These are all expressions that produce values:
1 + 2 + 3 // Resolves to: 6
"hello" // Resolves to: "hello"
isHappy ? "🙂" : "🙁" // Resolves to an emoji
[1, 2, 3].pop() // Resolves to: 3
Math.round(4.7) // Resolves to: 5
Statements Perform Actions
Statements are instructions that perform actions but don't produce values in the same way. Statements govern program flow and structure:
// These are statements that perform actions:
let x = 5; // Declares a variable
if (condition) { } // Conditionally executes code
for (let i = 0; i < 10; i++) { } // Loops through code
function myFunc() { } // Declares a function
The Console Test
A useful trick to determine whether code is an expression or statement: try logging it with console.log(). If the code runs and produces output showing a value, it's an expression. If you get an error, it's a statement.
Common Expression Statement Types
Not all expressions make useful expression statements. For an expression statement to be meaningful, the expression should produce side effects. Here are the most common types:
Function Calls
The most common form. Call functions for their side effects, not return values: console.log(), DOM methods, event listeners.
Assignment Expressions
Assign values while returning the assigned value: userName = "Alice", counter += 1, destructuring assignments.
Increment/Decrement
Modify numeric values: index++, --counter, pageNumber--. Both postfix and prefix forms work as expression statements.
Delete Operator
Remove object properties: delete object.property. Returns true/false but we typically use it for the side effect.
1// Function calls for side effects2console.log("Processing complete");3document.getElementById("app").classList.add("active");4items.forEach(item => item.process());5 6// Assignment expressions7userName = "Alice";8counter += 1;9const { firstName, lastName } = user;10 11// Increment and decrement12index++;13++pageNumber;14userCount--;15 16// Other side-effect expressions17delete user.inactiveFlag;Forbidden Expressions and Ambiguity Resolution
Not every expression can serve as an expression statement. Certain expressions are forbidden because they would be ambiguous with other statement syntaxes.
1// ❌ ERROR: 'function' would be a declaration2function() { return 42; }();3 4// ✅ SOLUTION: Wrap in parentheses5(function() { return 42; })();6 7// ❌ ERROR: '{' would be a block statement8{ name: "Alice", age: 30 }9 10// ✅ SOLUTION: Wrap in parentheses for object literal11({ name: "Alice", age: 30 })12 13// ❌ ERROR: 'class' would be a declaration14class MyClass {}15 16// ✅ SOLUTION: Assign to a variable17const MyClass = class { };Practical Applications and React Implications
The distinction between expressions and statements becomes critically important in JSX-based frameworks like React. Within JSX curly braces {}, only expressions are valid--you cannot use statements directly. This is why understanding expression statements is essential for modern web development with React and Next.js.
When working with React components, you'll frequently encounter scenarios where you need to choose between expressions and statements. The JSX syntax essentially creates expression slots within markup, making the expression/statement distinction a practical daily concern. Following consistent CSS naming conventions and code style guidelines helps maintain clarity across your codebase.
1// ✅ Works: ternary is an expression2return (3 <div>4 {isLoggedIn ? <UserProfile /> : <LoginForm />}5 </div>6);7 8// ❌ ERROR: if statement is a statement, not an expression9return (10 <div>11 {if (isLoggedIn) { <UserProfile /> }} // Syntax error!12 </div>13);14 15// ✅ Solutions: use expressions instead16{isLoggedIn && <UserProfile />}17{isLoggedIn ? <UserProfile /> : null}Performance Considerations
Unnecessary Expression Evaluation
Expression statements evaluate their expressions fully, even when the result is discarded. Pure expressions (those without side effects) in expression statement positions are essentially no-ops:
// This runs the calculation but discards the result
// The expensive computation still happens!
const result = expensiveCalculation();
// This is a no-op - does nothing meaningful
expensiveCalculation();
Cost Awareness
Some side effects are more expensive than others:
| Operation | Cost | Example |
|---|---|---|
| Increment/Assignment | Low | counter++, x = 1 |
| DOM Manipulation | Medium | element.classList.add() |
| I/O Operations | High | console.log(), fetch() |
Understanding these costs helps you write performant JavaScript that balances readability with efficiency.
Best Practices
- Use expression statements for side effects - Function calls, assignments, increments are ideal
- Respect the expression/statement separation - Use expressions where values are needed, statements for control flow
- Use parentheses for ambiguous cases - Wrap IIFEs and object literals to avoid syntax errors
- Prefer expressions in JSX contexts - Use &&, ternary, and other expressions instead of statements
- Avoid pure expressions in statement positions - They run but accomplish nothing
Following these practices ensures your code is both correct and maintainable, whether you're building simple scripts or complex React applications.
Summary
Expression statements form a fundamental building block of JavaScript programs. Key takeaways:
- Expression statements evaluate expressions but discard results - ideal for side-effect operations
- Expressions produce values; statements perform actions - understanding this distinction is crucial
- Forbidden expressions (starting with
function,async function,class,{, orlet[) require parentheses - Function calls, assignments, and increment/decrement operators are the most common expression statement types
- The distinction is critical in JSX and other expression-only contexts
- Pure expressions in expression statement positions are no-ops and indicate errors or oversight
Mastering expression statements helps you write more intentional, idiomatic JavaScript that works correctly across all contexts, from vanilla JS to React and Next.js applications.
Frequently Asked Questions
What is the difference between an expression and a statement?
Expressions produce values (like 1 + 2 produces 3), while statements perform actions (like declaring a variable or creating a loop). Expression statements are expressions placed where statements are expected.
Why are expression results discarded?
Expression statements are designed for operations that matter for what they do, not what they return. For example, console.log() outputs text--its return value of undefined isn't useful.
What are forbidden expressions?
Expressions starting with 'function', 'async function', 'class', '{', or 'let[' are forbidden because they would be ambiguous with declarations or block statements. Wrap them in parentheses to use as expression statements.
Why do I need parentheses around IIFEs?
JavaScript's parser sees 'function' at the start and expects a function declaration. Parentheses force it to parse as a function expression instead, allowing immediate invocation.
Sources
- MDN Web Docs: Expression Statement - Official JavaScript reference for expression statement syntax and semantics
- MDN Web Docs: Expressions and Operators - Comprehensive operator and expression documentation
- Josh W. Comeau: Statements vs Expressions - Educational explanation with practical examples