javascript

Expression Statements

Learn what expression statements are in JavaScript. Understand how expressions create values and how statements perform actions, forming the core of your code.

9 min read 8 sections Tutorial
Share

In JavaScript, everything you write is either an expression or a statement. An **expression statement** is a special kind of statement that performs an action using a value created by an expression. Understanding them helps you write clear and effective code.

Expression Statements

Expression Statements

Every line of JavaScript code does something. It either calculates a value or tells the computer to perform an action. An expression statement is a combination of these two ideas.

It starts with an expression, which is a piece of code that produces a value. Then, it uses that value to perform an action, making it a complete instruction or statement.

What is an Expression Statement?

An expression statement is a JavaScript statement that consists of an expression followed by a semicolon ;. It causes the JavaScript engine to evaluate the expression and then discard the result, or use its side effects.

Expressions

Code that produces a value, like 5 + 3 or userName. They are not full commands.

Statements

Complete instructions that perform an action, like let x = 10; or if (...) {...}.

Combining Them

An expression statement uses an expression to perform a command, often ending with a semicolon.

Why They Matter

They are the building blocks for assigning values, calling functions, and making your code do things.

What is an Expression?

Getting Started

What is an Expression?

Before we talk about expression statements, let's understand an expression. An expression is any piece of code that JavaScript can calculate to produce a single value. Think of it like a math problem that has an answer.

For example, 5 + 3 is an expression because it calculates to 8. A variable name like userName is also an expression, because it calculates to the value stored inside that variable.

javascript
1// These are all expressions because they produce a value:
2
35; // This expression produces the number 5
4"Hello, world!"; // This expression produces the text string "Hello, world!"
5
6let x = 10;
7x; // This expression produces the value of x, which is 10
8
95 + 3; // This expression produces the number 8
10
11Math.random(); // This expression produces a random number between 0 and 1
12
13(function() { // This is a function expression that produces a function
14 return 'Hi';
15})(); // Calling the function produces the string 'Hi'

Expressions by themselves do not do anything

If you just write 5 + 3; in your code without assigning it to a variable or using it in some way, JavaScript will calculate 8 and then immediately forget it. An expression needs to be part of a larger instruction to be useful.

What is a Statement? And an Expression Statement?

What is a Statement? And an Expression Statement?

A statement is a complete instruction that tells the computer to perform an action. It's like a full sentence in a programming language. Statements often end with a semicolon ;.

An expression statement is simply an expression that you use as a statement. You take an expression that produces a value, and then you use it to do something. The most common expression statements are assigning values to variables or calling functions.

javascript
1// This is an expression statement:
2// The expression is '10', and assigning it to 'score' performs an action.
3let score = 10;
4
5// This is also an expression statement:
6// The expression is 'score + 5', and assigning its result to 'score' is an action.
7score = score + 5;
8
9// This is a function call expression statement:
10// The expression is 'console.log("Hello")', which produces undefined,
11// but its side effect is printing text.
12console.log("Hello");
13
14// Another function call expression statement
15alert("Welcome!");
✗ BadJust an Expression (no action)
5 + 3; // JavaScript calculates 8, then forgets it
"Hello"; // JavaScript creates the string, then forgets it
Math.max(1, 5); // JavaScript calculates 5, then forgets it
✓ GoodExpression Statement (performs action)
let result = 5 + 3; // Assigns 8 to 'result'
console.log("Hello"); // Prints "Hello" to the console
let highest = Math.max(1, 5); // Assigns 5 to 'highest'

Common Types of Expression Statements

Common Types of Expression Statements

Expression statements are everywhere in JavaScript code. They are fundamental for making your program actually do things. The most frequent uses are assigning values and calling functions, but they also include incrementing numbers or creating new objects.

Assignment Expression Statements
// Assigning a value to a variable
let userAge = 30;
// Updating a variable's value
userAge = userAge + 1;
// Using shorthand assignment
let counter = 0;
counter += 5;
VS
Function Call Expression Statements
// Calling a function to perform an action
console.log("User logged in!");
// Calling a method on an object
Math.sqrt(25);
// Calling a function that returns a value, but we don't store it
// The function still runs and might have side effects
confirm("Are you sure?");

Side Effects are Key

Many expression statements are useful because of their side effects. For example, console.log() doesn't return a value you usually care about, but its side effect is displaying text on the screen. Assigning a value to a variable also has the side effect of changing the variable's state.

Using Expression Statements in a Real Program

Using Expression Statements in a Real Program

Let's see how expression statements work together in a simple scenario. Imagine you are building a small web page that tracks a user's clicks. Each click updates a counter and displays a message. This involves multiple expression statements working in sequence.

1

Initialize a counter

Start by declaring a variable to hold the click count. This is an assignment expression statement.

javascript
1let clickCount = 0;
2

Define an update function

Create a function that will run when a click happens. Inside, we'll use expression statements.

javascript
1function handleClick() {
2 clickCount = clickCount + 1; // Assignment expression statement
3 console.log('Clicks:', clickCount); // Function call expression statement
4 alert('You clicked ' + clickCount + ' times!'); // Another function call
5}
3

Simulate a click

Call the handleClick function. This is a function call expression statement that triggers all the actions inside.

javascript
1handleClick(); // First click
2handleClick(); // Second click
4

Check the final state

After the clicks, the clickCount variable holds the updated value thanks to the expression statements.

javascript
1console.log('Final count:', clickCount); // Will print 2

Missing Semicolons Can Cause Problems

While JavaScript often tries to guess where semicolons should go (this is called Automatic Semicolon Insertion or ASI), relying on it can lead to unexpected bugs. Always end your expression statements with a semicolon ; for clarity and safety, especially when they are on separate lines.

Expression vs. Statement vs. Expression Statement

Expression vs. Statement vs. Expression Statement

ConceptWhat it DoesExampleKey Characteristic
ExpressionProduces a value`10 + 5`, `userName`, `myFunction()`Evaluates to a single result
StatementPerforms an action`if (x > 0) {}`, `for (...) {}`, `let y = 20;`A complete instruction
Expression StatementAn expression used as a statement to perform an action (often with side effects)`x = 10;`, `console.log('Hi');`, `user.save();`An expression followed by a semicolon, causing evaluation and action

Test Your Knowledge

Test Your Knowledge

Test Your Knowledge

Quick Check

Which of the following is an expression?

Quick Check

Which of the following is an expression statement?

Quick Check

What is the primary characteristic of an expression?

Quick Check

What happens if you write console.log("Done");?

Quick Reference

Quick Reference

Quick Reference

Pro Tips
  • 1Expression — a piece of code that evaluates to a single value. Example: 10 * 5.
  • 2Statement — a complete instruction that performs an action. Example: if (condition) {}.
  • 3Expression Statement — an expression used as a complete instruction, usually ending with a semicolon. Example: myVariable = 20;.
  • 4Semicolons — always use them to end expression statements for clarity and to prevent unexpected behavior.
  • 5Assignmentx = 5; is a common assignment expression statement.
  • 6Function Callsconsole.log('Hello'); is a common function call expression statement.
  • 7Side Effects — many expression statements are valuable because they change something (like a variable's value) or produce output.
  • 8Discarded Value — the value produced by an expression statement is often not used, but the action it performs (its side effect) is the main goal.
  • 9Not all statements are expressions — control flow statements like if, for, while do not produce values and are not expression statements.
  • 10Foundational — expression statements are basic building blocks for nearly every JavaScript program.

Control Flow

Learn how if/else, loops, and switch statements direct your program's execution.

Data Types

Understand the different kinds of values JavaScript can work with: numbers, strings, booleans, objects.

Operators

Explore how arithmetic, comparison, and logical operators manipulate values in expressions.

Functions

Dive deeper into how to create and call functions, which are often used as expression statements.

You now understand Expression Statements!

You've learned the difference between expressions and statements, and how expression statements combine them to make your code perform actions. This fundamental concept is crucial for writing any interactive JavaScript program.

Try it in the Javascript Compiler

Run and experiment with Javascript code right in your browser — no setup needed.

Continue Learning