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?
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.
1// These are all expressions because they produce a value:235; // This expression produces the number 54"Hello, world!"; // This expression produces the text string "Hello, world!"56let x = 10;7x; // This expression produces the value of x, which is 10895 + 3; // This expression produces the number 81011Math.random(); // This expression produces a random number between 0 and 11213(function() { // This is a function expression that produces a function14 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.
1// This is an expression statement:2// The expression is '10', and assigning it to 'score' performs an action.3let score = 10;45// This is also an expression statement:6// The expression is 'score + 5', and assigning its result to 'score' is an action.7score = score + 5;89// 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");1314// Another function call expression statement15alert("Welcome!");
5 + 3; // JavaScript calculates 8, then forgets it"Hello"; // JavaScript creates the string, then forgets itMath.max(1, 5); // JavaScript calculates 5, then forgets it
let result = 5 + 3; // Assigns 8 to 'result'console.log("Hello"); // Prints "Hello" to the consolelet 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.
// Assigning a value to a variablelet userAge = 30;// Updating a variable's valueuserAge = userAge + 1;// Using shorthand assignmentlet counter = 0;counter += 5;
// Calling a function to perform an actionconsole.log("User logged in!");// Calling a method on an objectMath.sqrt(25);// Calling a function that returns a value, but we don't store it// The function still runs and might have side effectsconfirm("Are you sure?");
// Assigning a value to a variablelet userAge = 30;// Updating a variable's valueuserAge = userAge + 1;// Using shorthand assignmentlet counter = 0;counter += 5;
// Calling a function to perform an actionconsole.log("User logged in!");// Calling a method on an objectMath.sqrt(25);// Calling a function that returns a value, but we don't store it// The function still runs and might have side effectsconfirm("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.
Initialize a counter
Start by declaring a variable to hold the click count. This is an assignment expression statement.
1let clickCount = 0;
Define an update function
Create a function that will run when a click happens. Inside, we'll use expression statements.
1function handleClick() {2 clickCount = clickCount + 1; // Assignment expression statement3 console.log('Clicks:', clickCount); // Function call expression statement4 alert('You clicked ' + clickCount + ' times!'); // Another function call5}
Simulate a click
Call the handleClick function. This is a function call expression statement that triggers all the actions inside.
1handleClick(); // First click2handleClick(); // Second click
Check the final state
After the clicks, the clickCount variable holds the updated value thanks to the expression statements.
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
| Concept | What it Does | Example | Key Characteristic |
|---|---|---|---|
| Expression | Produces a value | `10 + 5`, `userName`, `myFunction()` | Evaluates to a single result |
| Statement | Performs an action | `if (x > 0) {}`, `for (...) {}`, `let y = 20;` | A complete instruction |
| Expression Statement | An 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
Which of the following is an expression?
Which of the following is an expression statement?
What is the primary characteristic of an expression?
What happens if you write console.log("Done");?
Quick Reference
Quick Reference
- 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.
- 5Assignment —
x = 5;is a common assignment expression statement. - 6Function Calls —
console.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,whiledo 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.