JavaScript · Lesson 3 of 20 · 4 min
Operators and Expressions
This is about showing you can write clean, bug-free logic and that you won't fall for the classic == vs === trap.
Nail the == vs === question
This is the most common question on this topic. Get it right and you signal you've moved past beginner-level bugs.
The difference is that == (loose equality) performs type coercion, while === (strict equality) does not. === checks if both the value and the type are the same. Because type coercion has confusing rules, == can lead to unexpected results.
Interviewer: "What's the difference between double and triple equals?" You: "
===checks for value and type equality without type coercion.==performs type coercion before comparing values. I always use===to avoid unpredictable behavior from coercion, like0 == falseevaluating to true."
The trap: An interviewer might ask for a "good" use case for ==. The only widely accepted one is x == null, which is true if x is null or undefined. It's a niche shortcut. The safer, more explicit answer is x === null || x === undefined. Stick to ===.
Use logical operators for more than just booleans
Logical operators (&&, ||) don't always return true or false. They return one of their operands, which lets you use them for control flow and setting default values. This is called short-circuiting.
A && B: If A is falsy, the expression short-circuits and returns A. Otherwise, it evaluates and returns B. This is a concise way to run code only if a condition is met.
A || B: If A is truthy, the expression short-circuits and returns A. Otherwise, it evaluates and returns B. This is the classic way to provide a default value.
The modern alternative: The nullish coalescing operator (??) is usually better for defaults. It only triggers for null and undefined, not for other falsy values like 0, "", or false.
const itemsInCart = data.items ?? 0; // Correctly handles case where data.items is 0
const itemsInCartLegacy = data.items || 0; // Bug: if data.items is 0, it becomes 0
Mentioning ?? shows your knowledge is current.
Use the ternary operator for concise assignments
The ternary operator (condition ? valueIfTrue : valueIfFalse) is a clean substitute for a simple if/else that assigns a value to a variable.
Use it for clarity, not just to be clever. If the logic is simple, a ternary is more readable.
const theme = isDarkMode ? "dark" : "light";
This is better than:
let theme;
if (isDarkMode) {
theme = "dark";
} else {
theme = "light";
}
The trap: Avoid nesting ternaries. They are unreadable and a red flag in a code review or interview. If you have an if/else if/else, use that instead.
Know the other operators and their quirks
You won't get deep questions on these, but you should know the common "gotchas."
Unary Plus for Type Coercion: The + operator can be used to quickly convert a string to a number. It's a bit of a code-golf trick, but it shows you know the language's quirks.
const stringId = "123";
const numericId = +stringId; // numericId is now the number 123
Increment/Decrement Precedence: The position of ++ or -- matters.
x++(postfix): returns the value ofx, then incrementsx.++x(prefix): incrementsx, then returns the new value. This is a classic trivia question. In real code, just writex += 1on its own line to avoid confusion.
Operator Precedence: JavaScript has an order of operations (like PEMDAS). Multiplication (*) comes before addition (+). The real pro move is to use parentheses () to make the order explicit and your code readable. Readability always wins.
A practical example
This function combines these concepts to create a user greeting. It handles missing data and formats the output based on user status, demonstrating good defaults, strict checks, and readable conditional logic.
function createUserGreeting(user) {
// Use `??` for a safe default. Handles null or undefined user object.
const userProfile = user ?? {};
// Use `||` for a default name. An empty string is fine to treat as "Guest".
const name = userProfile.name || "Guest";
// Use `===` for a strict, predictable comparison.
const isAdmin = userProfile.role === "admin";
// Use the ternary operator for a concise conditional assignment.
// This is cleaner than trying to use `&&` inside a template literal.
const roleText = isAdmin ? " (Admin)" : "";
const fullGreeting = `Welcome, ${name}${roleText}`;
return fullGreeting;
}
// Test cases
const adminUser = { name: "Alice", role: "admin" };
console.log(createUserGreeting(adminUser)); // "Welcome, Alice (Admin)"
const regularUser = { name: "Bob", role: "user" };
console.log(createUserGreeting(regularUser)); // "Welcome, Bob"
const guestUser = {};
console.log(createUserGreeting(guestUser)); // "Welcome, Guest"
const nullUser = null;
console.log(createUserGreeting(nullUser)); // "Welcome, Guest"
Try it
Find a simple if/else block in one of your projects that assigns a value to a variable and refactor it to use the ternary operator. Then, find where you check if a variable exists before using it and refactor that to use || or ?? for a default value.
Next: Now that you can create expressions, you'll learn to use them to control which code runs with "Control Flow: Conditionals".
Lock it in — apply this lesson under interview conditions.