⬅ Previous Topic
JavaScript OperatorsNext Topic ⮕
JavaScript Conditional Statements⬅ Previous Topic
JavaScript OperatorsNext Topic ⮕
JavaScript Conditional StatementsIn JavaScript, an expression is any piece of code that produces a value. Whether it’s as simple as a number, a string, or something more complex involving functions and operations — if it returns a value, it’s an expression.
Expressions are the building blocks of logic in JavaScript. Without them, statements can’t do anything meaningful. They fuel conditionals, loops, function calls — virtually every line of code depends on them.
Literals are the simplest form of expressions — they represent fixed values.
42
"Hello World"
true
null
These are values themselves — directly usable.
These use arithmetic operators to evaluate numbers.
5 + 3 * 2
11
Note: Operator precedence applies — multiplication before addition.
Combining strings using the +
operator.
"Hello" + " " + "World"
"Hello World"
Involve logical operators &&
, ||
, and !
for boolean logic.
true && false
false
These not only assign a value but are themselves expressions that return the value assigned.
let x = 10;
let y = (x = 20);
x is now 20, and y is also 20
You can define functions as expressions — especially common with anonymous and arrow functions.
const greet = function(name) {
return "Hello " + name;
}
greet("Alice") returns "Hello Alice"
Shortcut for if-else
logic.
let age = 18;
let status = (age >= 18) ? "Adult" : "Minor";
"Adult"
An expression produces a value. A statement performs an action.
// Expression
3 + 4
// Statement
let result = 3 + 4;
function double(n) {
return n * 2;
}
console.log(double(4 + 1));
10
The expression 4 + 1
is evaluated before being passed to double
.
let name = "Eva";
let greeting = `Hi, ${name.toUpperCase()}!`;
"Hi, EVA!"
Use parentheses to alter the default precedence.
(5 + 3) * 2
16
Wrap a function expression and call it instantly.
(function() {
console.log("IIFE executed!");
})();
IIFE executed!
JavaScript's eval()
can evaluate strings as expressions — though it should be avoided in production due to security risks.
let result = eval("10 + 5");
console.log(result);
15
Expressions are the heartbeat of JavaScript. As you continue your journey, you'll start recognizing expressions even in the most complex code. Understanding them makes debugging easier, optimizes your logic, and brings clarity to the art of programming.
In the next module, we'll explore JavaScript Operators — an essential companion to expressions — where we decode how operations work under the hood.
⬅ Previous Topic
JavaScript OperatorsNext Topic ⮕
JavaScript Conditional StatementsYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.