Deep dive into operators
Operators are the symbols and keywords that take one or more values, called operands, and produce a new value. JavaScript has many families of them beyond basic maths, and knowing how each one behaves, especially with mixed types, helps you read other people's code and avoid subtle bugs.
Operators grouped by what they do
| Family | Examples | Produces |
|---|---|---|
| Arithmetic | + - * / % ** | A number (or a string for + with text) |
| Assignment | = += -= ??= ||= | Stores a value in a variable |
| Comparison | === !== < >= | A boolean |
| Logical | && || ! | One of the operands, or a boolean for ! |
| Nullish and optional | ?? ?. | A fallback value or undefined |
| Type and others | typeof instanceof in ... ?: | Varies |
Operators also differ by how many operands they take. ! and typeof are unary (one operand), most are binary (two), and the conditional ? : is the only ternary operator.
Logical operators return values, not just true or false
This surprises many learners. a || b returns a if it is truthy, otherwise b. a && b returns a if it is falsy, otherwise b. They also short-circuit: the right side is not evaluated at all when the left side already decides the result. That is why user && user.name is safe even when user is null.
The falsy values are false, 0, -0, 0n, "", null, undefined and NaN. Everything else, including "0", empty arrays and empty objects, is truthy.
?? versus ||, and optional chaining
Using || for defaults has a hidden flaw: it replaces every falsy value. If a user genuinely sets a quantity of 0, qty || 1 turns it into 1. The nullish coalescing operator ?? only falls back when the left side is null or undefined, so qty ?? 1 keeps the 0.
Optional chaining ?. stops and returns undefined if the value before it is null or undefined, instead of throwing a TypeError.
const settings = { volume: 0, theme: null, profile: { city: 'Pune' } };
console.log(settings.volume || 50); // 50 (0 was treated as "missing")
console.log(settings.volume ?? 50); // 0 (0 is kept)
console.log(settings.theme ?? 'light'); // 'light'
console.log(settings.profile?.city); // 'Pune'
console.log(settings.address?.city); // undefined, no error
let retries;
retries ??= 3; // assigns only if null/undefined
console.log(typeof retries); // 'number'
console.log('volume' in settings); // true
Type-checking operators and their quirks
typeofreturns a string such as"number","string","object"or"function". Watch out:typeof nullis"object", a long-standing quirk, and arrays also report"object". UseArray.isArray()for arrays.instanceofchecks whether an object was created from a particular class or constructor, such aserr instanceof TypeError.inchecks whether a property name exists on an object, even if its value isundefined.
A mixing mistake the language blocks
You cannot combine ?? directly with || or && without parentheses. Writing a || b ?? c is a syntax error on purpose, because the intended order would be unclear. Add brackets: (a || b) ?? c. More generally, when several operators share a line, brackets make the meaning obvious to the next reader, even if the precedence rules from the previous lesson would give the same result.
Main points
&&and||return one of their operands and skip the right side when they can.- Use
??for defaults when 0 or an empty string are valid values. - Use
?.to read nested data safely, and remember thattypeof nullis"object".
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.