- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Expressions
JavaScript Statement & Expression
JavaScript Expressions
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
Example
javascript
10 + 20This is an expression because it produces the value 30.
Another example:
Example
javascript
let total = price * quantity;Here, price * quantity is an expression because it calculates a value.
Expression Meaning
An expression always gives a result.
Example
javascript
let age = 25;Here, 25 is an expression because it gives a value.
Example
javascript
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.
Example
javascript
let result = 10 + 20;In this code:
| Part | Type | Meaning |
|---|---|---|
10 + 20 | Expression | Produces the value 30 |
let result = 10 + 20; | 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.
Example
javascript
let a = 10;
let b = 5;
let sum = a + b;
let difference = a - b;
let product = a * b;In this example:
| Expression | Result |
|---|---|
a + b | Adds two values |
a - b | Subtracts one value from another |
a * b | Multiplies two values |
Numeric expressions are commonly used in calculations.
String Expressions
String expressions produce text values.
Example
javascript
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.
Example
javascript
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.
More examples:
Example
javascript
10 > 5
20 === 20
5 < 3These expressions return boolean values.
Assignment Expressions
An assignment expression assigns a value to a variable.
Example
javascript
let name = "Amit";Here, "Amit" is assigned to the variable name.
Another example:
Example
javascript
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.
Example
javascript
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.
Example
javascript
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 ${}.
Example
javascript
let name = "Amit";
let age = 25;
let message = `${name} is ${age} years old.`;You can also use calculations inside ${}.
Example
javascript
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
Complete Example
html
<!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
trueorfalse. - 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.
