Operators and Expressions
What is an Operator?
An operator is a symbol that performs an operation on one or more values. The values an operator works with are called operands. The result of an operation is called an expression.
5 + 3 // operator: +, operands: 5 and 3, result: 8
Arithmetic Operators
Arithmetic operators perform mathematical calculations:
const a = 10;
const b = 3;
console.log(a + b); // 13 — addition
console.log(a - b); // 7 — subtraction
console.log(a * b); // 30 — multiplication
console.log(a / b); // 3.333... — division
console.log(a % b); // 1 — modulus (remainder after division)
console.log(a ** b); // 1000 — exponentiation (10 to the power of 3)
The modulus operator (%) is especially useful for checking if a number is even or odd:
const num = 8;
if (num % 2 === 0) {
console.log("Even");
} else {
console.log("Odd");
}
Increment and Decrement
let count = 0;
count++; // count is now 1 (post-increment)
count--; // count is now 0 (post-decrement)
++count; // count is now 1 (pre-increment)
Comparison Operators
Comparison operators compare two values and return a boolean (true or false).
Loose Equality vs Strict Equality
This is one of the most important distinctions in JavaScript:
// == (loose equality) — converts types before comparing
console.log(5 == "5"); // true (number coerced to string)
console.log(0 == false); // true (both coerce to 0)
console.log(null == undefined); // true
// === (strict equality) — no type conversion, checks type AND value
console.log(5 === "5"); // false (different types)
console.log(0 === false); // false (different types)
console.log(5 === 5); // true
Always use === and !== in your code. Loose equality (==) can produce surprising results.
console.log(10 > 5); // true
console.log(10 < 5); // false
console.log(10 >= 10); // true
console.log(10 <= 9); // false
console.log(5 !== 5); // false
console.log(5 !== 6); // true
Logical Operators
Logical operators combine or invert boolean expressions.
AND (&&)
Returns true only if both sides are truthy:
const isLoggedIn = true;
const isPremium = true;
console.log(isLoggedIn && isPremium); // true
console.log(isLoggedIn && false); // false
OR (||)
Returns true if at least one side is truthy:
const hasEmail = false;
const hasPhone = true;
console.log(hasEmail || hasPhone); // true
console.log(false || false); // false
NOT (!)
Inverts a boolean value:
const isActive = true;
console.log(!isActive); // false
console.log(!false); // true
Short-Circuit Evaluation
JavaScript evaluates && and || lazily — it stops as soon as it knows the result.
// && stops at the first falsy value
console.log(null && "hello"); // null (stopped early)
console.log("hello" && "world"); // "world" (both truthy)
// || returns the first truthy value
console.log(null || "default"); // "default"
console.log("user" || "guest"); // "user"
Short-circuit evaluation is commonly used for default values:
const username = userInput || "Guest";
// If userInput is empty or null, use "Guest"
The Ternary Operator
The ternary operator is a concise way to write a simple if/else:
// Syntax: condition ? valueIfTrue : valueIfFalse
const age = 20;
const status = age >= 18 ? "Adult" : "Minor";
console.log(status); // "Adult"
// Equivalent if/else:
let status2;
if (age >= 18) {
status2 = "Adult";
} else {
status2 = "Minor";
}
Use the ternary operator for simple conditions. Use full if/else for complex logic to keep code readable.
Assignment Operators
Assignment operators update a variable's value:
let x = 10;
x += 5; // x = x + 5 → 15
x -= 3; // x = x - 3 → 12
x *= 2; // x = x * 2 → 24
x /= 4; // x = x / 4 → 6
x %= 4; // x = x % 4 → 2
Key Takeaways
- Arithmetic operators include +, -, *, /, % (modulus), and ** (exponentiation)
- Always use === (strict equality) instead of == (loose equality) to avoid type coercion surprises
- Logical operators: && (AND), || (OR), ! (NOT)
- Short-circuit evaluation stops processing as soon as the result is determined
- The ternary operator (condition ? a : b) is a concise alternative to if/else for simple cases
Practice Exercise
- Declare two variables:
price = 49.99andquantity = 3 - Calculate the total and log it
- Use the modulus operator to check if
quantityis even or odd - Use a ternary to set a
discountvariable to 10 if total > 100, otherwise 0 - Use logical operators to check if price > 20 AND quantity > 2, then log the result
Try it yourself
Key Takeaways
- Arithmetic operators (+, -, *, /, %, **) perform mathematical calculations on numbers
- Always use === (strict equality) to avoid unexpected type coercion bugs
- Logical operators && (AND), || (OR), and ! (NOT) combine or invert boolean expressions
- Short-circuit evaluation means && stops at the first falsy value and || stops at the first truthy value
- The ternary operator (condition ? a : b) is a concise one-line alternative to if/else
Quick Quiz
1.What does the modulus operator (%) return?
2.What is the difference between == and === in JavaScript?
3.What will console.log(null || "default") output?
4.What does the ternary expression age >= 18 ? "Adult" : "Minor" evaluate to when age is 16?
Ready to go further?
CareerEx gives you structured 12-week training, live classes every Saturday and Sunday, real tutor feedback, and a certificate. Join the next cohort.
Join CareerEx