Understanding Statement vs Expression
In JavaScript, we have a number of different ways to write code. Two of these ways are JavaScript statements and JavaScript expressions. While these two concepts are related, they are not interchangeable. Understanding the difference between them, and when to use each, is an important part of writing effective JavaScript code.
JavaScript Statements
A JavaScript statement is a piece of code that performs a specific action, such as declaring a variable, making a decision, or looping through data. Statements are executed line by line, in the order they appear in the code.
let x = 10; // variable declaration
if (x > 5) { // decision statement
console.log("x is greater than 5");
}
for (let i = 0; i < 10; i++) { // loop statement
console.log(i);
}
Statements are executed by the JavaScript engine, and they do not return any value.
In other words, statements don't produce any output, but they can have side effects, like declaring variables or changing values.
JavaScript Expressions
A JavaScript expression is a piece of code that returns a value. Expressions are used to produce output, and they can be used as part of larger statements or as standalone code. Here are some examples of JavaScript expressions:
let x = 10;
let y = x + 5; // expression that returns the value 15
console.log(y); // outputs "15"
Expressions can be made up of variables, operators, and other expressions. When the JavaScript engine evaluates an expression, it produces a value. This value can be used in other expressions or statements.
Edge-Case Uses
While statements and expressions serve different purposes, there are some edge cases where they can be used interchangeably.
For example, an expression can be used as a statement. This is often done when the value returned by the expression is not needed.
let x = 10;
x + 5; // expression that returns the value 15, but is not used
Similarly, a statement can be used as an expression by wrapping it in parentheses. This is often done to make use of the side effects of the statement, rather than its return value. For example:
let x = 10; (x = x + 5); // statement that increments x by 5, but does not return any value
console.log(x); // outputs "15"
Conclusion
Understanding the difference between JavaScript statements and expressions, and when to use each, is a key aspect of writing effective JavaScript code. Statements are used to perform actions, while expressions are used to produce values. While they can be used interchangeably in some cases, it's important to understand the fundamental differences between them.
Subscribe to my newsletter
Read articles from Dhawal Pandya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Dhawal Pandya
Dhawal Pandya
Engineering @ Razorpay