Statement vs expression in JavaScript

Srijan } Author Pic
SrijanJun 08, 2025 - JavaScript
Statement vs expression in JavaScript - Reacted Node

Photo: Statement vs Expression - ReactedNode

What You Should Know

  • Understand how to declare and use variables with let, const, or var.
  • Be aware of basic types like numbers, strings, and booleans.
  • Know how to perform simple arithmetic and string operations.

What You Will Learn

  • What are expressions in JavaScript, and how do expressions evaluate to values and are used
  • What are statements in JavaScript, and how do statements perform actions and control the flow of code
  • The key difference between expressions and statements in JavaScript

Key Concepts

Expressions

An expression is any valid unit of code that produces a value.

Example:

122 + 55; // evaluates to 77
2("reacted node"); // evaluates to 'reacted node'
3true && false; // evaluates to false
4myFunc(); // return value of myFunc

Expressions in JavaScript have the following characteristics:

  • It always returns a value
  • It can be used anywhere a value is expected (e.g., as part of an assignment or function argument).
  • Can be simple or complex (literals, operators, function calls, etc.).

Simple Expressions

Simple expressions are small, straightforward pieces of code that evaluate to a single value. These don't involve any operations—just literal values or identifiers.

142; // evaluates to 42
2("Hello"); // evaluates to "Hello"
3true; // evaluates to true

Complex Expressions

Complex Expressions combine multiple values, operators, or function calls but still result in a single value.

Even though they're made up of smaller parts (which might be simple expressions themselves), they still evaluate to one final result.

122 + 55 * 2; // Evaluates to 132
2Math.max(10, 20); // Evaluates to 20
3(a && b) || c; // Evaluates based on logic, but still one final value
4user.age > 18 ? "Adult" : "Minor"; // "Adult" or "Minor"

Statements

A statement is a unit of code that performs an action.

1let x = 10; // variable declaration statement
2if (x > 5) { ... } // if statement
3for (let i = 0; i < 10; i++) { ... } // for loop statement
4function greet() { ... } // function declaration statement

Statements in JavaScript have the following characteristics:

  • Does not always return a value
  • Often controls the flow of the program
  • Can contain expressions inside them

Statements vs Expressions

TypeReturns a Value?Can Be Used as a Value?Purpose
ExpressionYesYesCompute or evaluate something
StatementNot alwaysNot directlyPerform an action or control flow
1// Expression
2let y = 5 + 3; // "5 + 3" is an expression. "let y = 5 + 3" is a statement.
3
4// Statement
5if (y > 5) {
6 console.log("y is greater than 5");
7}
8// The whole 'if' block is a statement; "y > 5" is an expression.

Why it matters

Misunderstanding expressions vs. statements can lead to subtle issues with features like arrow functions.

1const getUserName = () => {
2 "John Doe";
3};
4
5console.log(getUserName()); // undefined

It returned undefined because the curly braces turn it into a statement block, and without a return, nothing gets returned.

In the above example, undefined was returned by the function getUserName because the function's curly braces turn it into a statement block, and nothing gets returned without a return.

To correctly handle it, we can either add a return statement or rewrite them like this:

1const getUserName = () => "John Doe";
2// or
3const getUserName = () => {
4 return "John Doe";
5};

Apart from this, some JavaScript features like ternary operators and logical short-circuiting only work with expression. Knowing what qualifies as an expression lets you write cleaner logic.

1const age = 20;
2
3// both branches are expressions
4const message = age >= 18 ? "Adult" : "Minor";
5console.log(message); // "Adult"
6
7const isLoggedIn = true;
8
9// Expression used with &&
10isLoggedIn && console.log("Welcome back!"); // "Welcome back!"