Expressions Produce Values
Expressions Produce Values
An expression evaluates to a value that a statement can use. Multiplication groups before addition; parentheses can change that grouping. Distinguish the calculated value from the instruction that displays it.
Before this topic, review Operators and Comparisons if its terms are unfamiliar.
What you’ll learn
- Explain expressions produce values through the school example.
- Trace the shown operations and distinguish their result from displayed output.
- Make the practice change and explain its consequence: Without parentheses the product is added to 20; with parentheses 25 is multiplied by 2.
Work through the school example
This is JavaScript using language features and the host console. Read the initial values, then follow each operation to its displayed result. A declaration names a value, a function call performs an operation, and console.log displays its arguments.
JavaScript · school.js · complete example
console.log(10 + 5 * 2);
console.log((10 + 5) * 2);
Expected output
20
30Run in JavaScript compiler →What is a JavaScript Expression?
In 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.
Why Are Expressions Important?
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.
1. Types of JavaScript Expressions
1.1 Literal Expressions
Literals are the simplest form of expressions — they represent fixed values.
42
"Hello World"
true
null
These are values themselves — directly usable.
1.2 Arithmetic Expressions
These use arithmetic operators to evaluate numbers.
5 + 3 * 2
11
Note: Operator precedence applies — multiplication before addition.
1.3 String Expressions
Combining strings using the + operator.
"Hello" + " " + "World"
"Hello World"
1.4 Logical Expressions
Involve logical operators &&, ||, and ! for boolean logic.
true && false
false
1.5 Assignment Expressions
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
1.6 Function Expressions
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"
1.7 Conditional (Ternary) Expressions
Shortcut for if-else logic.
let age = 18;
let status = (age >= 18) ? "Adult" : "Minor";
"Adult"
2. Expression vs Statement
An expression produces a value. A statement performs an action.
// Expression
3 + 4
// Statement
let result = 3 + 4;
3. Advanced Concepts in Expressions
3.1 Expression as Function Arguments
function double(n) {
return n * 2;
}
console.log(double(4 + 1));
10
The expression 4 + 1 is evaluated before being passed to double.
3.2 Expressions in Template Literals
let name = "Eva";
let greeting = `Hi, ${name.toUpperCase()}!`;
console.log(greeting);
Hi, EVA!
3.3 Grouping Expressions with Parentheses
Use parentheses to alter the default precedence.
(5 + 3) * 2
16
3.4 Immediately Invoked Function Expressions (IIFE)
Wrap a function expression and call it instantly.
(function() {
console.log("IIFE executed!");
})();
IIFE executed!
4. Evaluating Expressions Dynamically
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
5. Tips for Understanding and Using Expressions
- Every time you write a value, you’re writing an expression.
- Every operation that results in a value is an expression.
- Function arguments, array elements, object values — they’re all built using expressions.
Recognize the important boundary
Grouping is not the same as changing every evaluation-order rule. Avoid expressions with several hidden mutations.
Try a small variation
JavaScript · original source for practice
console.log(10 + 5 * 2);
console.log((10 + 5) * 2);
Your task: Replace 10 with 20. Predict the result before running the edited program.
The source is repeated here. If the edit targets the HTML rather than the script, use the markup repeated below; the worked answer also repeats the changed markup.
Worked answer and reasoning
Without parentheses the product is added to 20; with parentheses 25 is multiplied by 2.
JavaScript · complete worked variation
console.log(20 + 5 * 2);
console.log((20 + 5) * 2);
Expected output
30
50Run in JavaScript compiler →Predict the original program
JavaScript · original program for this question
console.log(10 + 5 * 2);
console.log((10 + 5) * 2);
Keep the useful idea
An expression evaluates to a value that a statement can use. Multiplication groups before addition; parentheses can change that grouping. Distinguish the calculated value from the instruction that displays it.
Check your variation against this explanation: Without parentheses the product is added to 20; with parentheses 25 is multiplied by 2.
Use the JavaScript array reference library for individual array operations and boundary tests to check a rule independently.
Reaching the recap and completing the quizzes are separate milestones.