JavaScript Expressions and Operators
Learn how JavaScript expressions produce values, how operators combine and transform them, and how precedence and short-circuiting affect evaluation.
Basic
An expression is JavaScript syntax that is evaluated to produce a value. A statement is a larger syntactic unit that performs an action or controls execution. The distinction matters because expressions can appear inside many statements, including assignments, conditions, function calls, and returns.
1. Expressions produce values
A literal such as
42 is an expression. So is a variable reference such as
score and an operation such as
score + 10. A statement can contain an expression, but the two concepts are not interchangeable.
Output
Ready to run.
Quick check
Which part is the expression in const total = price * quantity;?
const total = ;
2. Assignment is an expression too
An assignment such as
score = 20 evaluates to the value that was assigned. That is why assignment can be nested in another expression. Prefer straightforward assignments when nesting would make the program harder to read.
Output
Ready to run.
Quick check
What value does (count = 3) produce?
const result = (count = );
3. Arithmetic and comparison are expressions
Arithmetic operators calculate numeric values. Comparison operators such as
=== and
< produce Boolean values. Keep the result in mind: an expression does not merely “do something”; it evaluates to a value.
Output
Ready to run.
Quick check
What type of value does 12 < 20 produce?
const result = 12 < 20;
console.log(result);
4. Logical operators can stop before evaluating the right side
&& and
|| combine Boolean-oriented conditions, but they also return one of their operands. They evaluate left to right and may short-circuit: with
&& a falsy left operand prevents the right operand from being evaluated; with
|| a truthy left operand does the same.
Output
Ready to run.
Quick check
How many times does check() run in false && check()?
const result = false && check();
5. Nullish coalescing answers a different question
?? uses the right operand only when the left operand is
null or
undefined. It is different from
|| because
|| also treats other falsy values, such as
0 and
"", as a reason to use its right operand.
Output
Ready to run.
Quick check
What does 0 ?? 10 evaluate to?
const result = 0 ?? 10;
6. Conditional and logical assignment operators express small decisions
The conditional operator
condition ? whenTrue : whenFalse chooses one of two expressions. Logical assignment operators such as
&&=,
||= and
??= combine a logical test with assignment. Use them when the compact form remains clear.
Output
Ready to run.
Quick check
Which operator assigns only when the left value is nullish?
let name = undefined;
name "Guest";
7. Grouping and precedence change how expressions are evaluated
Operators have defined precedence and associativity. Parentheses explicitly group an expression and are often the clearest way to communicate intent. Do not rely on memorized precedence when grouping makes the intended calculation clearer.
Output
Ready to run.
Quick check
What is the clearest way to force addition before multiplication in 2 + 3 * 4?
const result = ;
8. Other operators are part of the language, not magic syntax
JavaScript also defines operators and operator-like expressions such as
typeof,
delete,
void,
in and
instanceof. Bitwise operators work on integer-like bit patterns. Increment and decrement change a variable and produce a value; use them carefully when embedding side effects inside larger expressions.
Quick check
What does typeof 42 produce?
const type = typeof 42;
Practice what you learned
Use the expressions and operators code-completion exercise and then take the expressions and operators quiz.
Authoring rules to remember
Distinguish expressions that produce values from statements that organize execution.
Use parentheses when they make grouping or intent clearer.
Remember that &&, ||, and ?? return operands and can short-circuit.
Use ?? when only null and undefined should trigger a fallback.
Keep side effects out of dense expressions when they reduce readability.
Next step
Next, deepen the model of JavaScript values, types, coercion, and equality.
Thanks for your feedback.
Mark this learning resource complete to track your learning progress.