Skip to main contentJavaScript Expressions Tutorial - HTML5ANDCSS3JavaScript Statement & Expression
JavaScript Expressions
Written by Swapnil RajaPublished A JavaScript expression is a piece of code that produces a value.
Expressions can be simple or complex. They can include values, variables, operators, function calls, or a combination of these.
Example
This is an expression because it produces the value 30.
Another example:
let total = price * quantity;
Here, price * quantity is an expression because it calculates a value.
Expression Meaning
An expression always gives a result.
Here, 25 is an expression because it gives a value.
let fullName = "Amit" + " " + "Kumar";
Here, "Amit" + " " + "Kumar" is an expression because it produces a single string value.
Expression and Statement Difference
A statement is an instruction.
An expression is code that produces a value.
| Part | Type | Meaning |
|---|
<code>10 + 20</code> | Expression | Produces the value 30 |
<code>let result = 10 + 20;</code> | Statement | Stores the value in a variable |
So, an expression can be part of a statement.
Numeric Expressions
Numeric expressions are expressions that produce number values.
let a = 10;
let b = 5;
let sum = a + b;
let difference = a - b;
let product = a * b;
| Expression | Result |
|---|
<code>a + b</code> | Adds two values |
<code>a - b</code> | Subtracts one value from another |
<code>a * b</code> | Multiplies two values |
Numeric expressions are commonly used in calculations.
String Expressions
String expressions produce text values.
let firstName = "Amit";
let lastName = "Kumar";
let fullName = firstName + " " + lastName;
Here, firstName + " " + lastName is a string expression.
The + operator joins the string values together.
Boolean Expressions
Boolean expressions produce either true or false.
let age = 20;
let isAdult = age >= 18;
Here, age >= 18 is a boolean expression.
It checks whether the value of age is greater than or equal to 18.
These expressions return boolean values.
Assignment Expressions
An assignment expression assigns a value to a variable.
Here, "Amit" is assigned to the variable name.
let score = 10;
score = score + 5;
Here, score + 5 is calculated first, and then the result is assigned back to score.
Expressions with Variables
Expressions often use variables.
let price = 500;
let quantity = 3;
let total = price * quantity;
Here, price * quantity is an expression that uses two variables.
If the value of price or quantity changes, the result of the expression also changes.
Expressions with Functions
A function call can also be an expression if it returns a value.
function add(a, b) {
return a + b;
}
let result = add(10, 20);
Here, add(10, 20) is an expression because it returns a value.
Expressions in Template Literals
Expressions can be used inside template literals with ${}.
let name = "Amit";
let age = 25;
let message = `${name} is ${age} years old.`;
You can also use calculations inside ${}.
let price = 100;
let quantity = 4;
let message = `Total price is ${price * quantity}`;
Here, price * quantity is an expression inside the template literal.
Complete Example
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Expressions</title>
</head>
<body>
<h1>JavaScript Expressions</h1>
<p id="result"></p>
<script>
let price = 500;
let quantity = 3;
let total = price * quantity;
let message = "Total Price: " + total;
let isExpensive = total > 1000;
document.getElementById("result").innerHTML =
message + "<br>" +
"Is expensive: " + isExpensive;
</script>
</body>
</html>
Try It Yourself
Run the above example in the Try It Editor.
Try changing the values of price and quantity.
Also try creating your own numeric, string, and boolean expressions.
Important Points
- An expression is code that produces a value.
- Expressions can contain values, variables, operators, and function calls.
- Numeric expressions produce numbers.
- String expressions produce text.
- Boolean expressions produce
true or false. - Expressions are often used inside statements.
- A function call can also be an expression if it returns a value.
Conclusion
JavaScript expressions are used to produce values.
They are used in calculations, string joining, comparisons, assignments, function calls, and template literals.
Understanding expressions is important because they are used in almost every JavaScript program.