By the end of this lesson, you will be able to use JavaScript operators to calculate values, assign data, compare conditions, and combine logical tests in small programs.
What it is
Operators are symbols that tell JavaScript to perform a specific operation on one or more values, called operands. Arithmetic operators calculate numbers, assignment operators store values, comparison operators produce true or false, and logical operators combine boolean conditions. Related terms include expression, operand, precedence, and short-circuit evaluation.
Why it matters
- They let you transform user input into computed results, such as totals or averages.
- They control program flow through conditions like
ifstatements. - They make code shorter and clearer than writing helper functions for simple tasks.
- They support safe checks, such as comparing values without changing them.
- They combine multiple rules, such as requiring both age and permission before allowing access.
Syntax or steps
The smallest useful pattern is: operand operator operand. For example, 5 + 3 produces 8. Assignment uses = to store a result in a variable. Comparison uses === to test equality without type coercion. Logical operators use && for AND and || for OR.
Example
let age = 20;
let hasLicense = true;
let sum = 5 + 3;
let product = sum * 2;
let isAdult = age >= 18;
let canDrive = isAdult && hasLicense;
console.log(sum, product, isAdult, canDrive);
Part by part: let age = 20; assigns a number. let sum = 5 + 3; uses the arithmetic + operator. let product = sum * 2; multiplies the stored result. let isAdult = age >= 18; compares age and stores a boolean. let canDrive = isAdult && hasLicense; uses logical AND, so both conditions must be true. The output is 8 40 true true.
Common mistakes
- Using
=inside anifcondition. That assigns a value instead of comparing it. Use===for equality checks. - Using
==when you mean===. Loose equality can convert types, so"5" == 5is true, but"5" === 5is false. - Forgetting that
+concatenates strings."5" + 3becomes"53", not8. Convert withNumber()when needed. - Assuming logical operators always return booleans.
&&and||return one of their operands, which can be useful but surprising.
When to use it
Use operators for simple, direct work. Use methods or functions when the operation is complex, reusable, or needs clearer naming.
| Use operators when | Use methods or functions when |
|---|---|
| You need quick math, assignment, or boolean checks. | You need repeated logic, validation, or domain-specific behavior. |
| The expression is short and readable. | The expression becomes long or hard to understand. |
| You are comparing primitive values. | You are transforming arrays or objects. |
Practice
Guided exercise: create variables named price, quantity, and isMember. Calculate total with multiplication, then calculate discountedTotal using total * 0.9 only when isMember is true. Hint: use let discountedTotal = isMember ? total * 0.9 : total;.
Challenge: write a condition that checks whether a user is between 18 and 65 and has a valid email. Expected output: true when all rules pass, false otherwise.
Quick check
Question: What is the difference between 5 + 3 and "5" + 3?
Answer: 5 + 3 performs arithmetic addition and returns 8. "5" + 3 performs string concatenation and returns "53".
Summary
JavaScript operators are compact tools for calculating, storing, comparing, and combining values. Choosing the right operator makes code shorter and clearer, while using strict comparison and careful type conversion prevents common bugs.