1.18 — Operators and the Type System: Quick Revision
Compact cheat sheet. Print-friendly.
How to use this material (instructions)
- Skim before labs or interviews.
- Drill gaps — reopen
README.md → 1.18.a…1.18.g.
- Practice —
1.18-Exercise-Questions.md.
- Polish answers —
1.18-Interview-Questions.md.
Arithmetic operators
| Operator | Name | Example | Result |
|---|
+ | Addition / concat | 5 + 3 / "5" + 3 | 8 / "53" |
- | Subtraction | "10" - 3 | 7 |
* | Multiplication | 4 * 3 | 12 |
/ | Division | 7 / 2 | 3.5 |
% | Remainder | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
++x | Pre-increment | Returns new | x = x + 1 first |
x++ | Post-increment | Returns old | x = x + 1 after |
+ rule: If either operand is a string, concatenate. Other operators always convert to number.
Special numeric values
| Value | When it appears | Check |
|---|
NaN | Invalid math (0/0, "hello" * 2) | Number.isNaN(x) |
Infinity | 1/0 | Number.isFinite(x) |
-Infinity | -1/0 | Number.isFinite(x) |
typeof NaN === "number" and NaN !== NaN.
Comparison operators
| Operator | Name | Coerces? |
|---|
=== | Strict equality | No (default choice) |
!== | Strict inequality | No |
== | Loose equality | Yes |
!= | Loose inequality | Yes |
> < >= <= | Relational | Yes (to number, or lexicographic if both strings) |
Critical edge cases
null == undefined → true null === undefined → false
NaN === NaN → false Object.is(NaN, NaN) → true
0 === -0 → true Object.is(0, -0) → false
null >= 0 → true null == 0 → false
"10" < "9" → true (string comparison, not numeric)
Logical operators
| Operator | Returns | Short-circuits when |
|---|
&& | First falsy or last value | Left is falsy |
|| | First truthy or last value | Left is truthy |
! | Inverted boolean | — |
?? | Right if left is null/undefined | Left is NOT null/undefined |
Precedence: ! > && > || > ??
Cannot mix ?? with &&/|| without parentheses.
|| vs ?? quick comparison
| Expression | || | ?? |
|---|
0 op 42 | 42 | 0 |
"" op "default" | "default" | "" |
false op true | true | false |
null op "fallback" | "fallback" | "fallback" |
undefined op "fallback" | "fallback" | "fallback" |
Rule: Use ?? when 0, "", or false are valid values.
Assignment operators
| Operator | Meaning |
|---|
= | Basic assignment |
+= -= *= /= %= **= | Compound (operate + assign) |
||= | Assign if current is falsy |
&&= | Assign if current is truthy |
??= | Assign if current is null/undefined |
typeof results
| Value | typeof |
|---|
42 / NaN / Infinity | "number" |
"hello" | "string" |
true | "boolean" |
undefined | "undefined" |
null | "object" (bug) |
{} / [] | "object" |
function(){} | "function" |
Symbol() | "symbol" |
10n | "bigint" |
Type checking cheat sheet
| Check for... | Use |
|---|
| Array | Array.isArray(x) |
| Null | x === null |
| Null or undefined | x == null |
| Real object (not null, not array) | x !== null && typeof x === "object" && !Array.isArray(x) |
| Number (not NaN) | typeof x === "number" && !Number.isNaN(x) |
| Function | typeof x === "function" |
| Any type precisely | Object.prototype.toString.call(x) |
The 8 falsy values
false 0 -0 0n "" null undefined NaN
Everything else is truthy — including [], {}, "0", "false", " ".
Type coercion rules
To Number
| From | Result |
|---|
"" / " " | 0 |
"42" | 42 |
"42px" | NaN |
true / false | 1 / 0 |
null | 0 |
undefined | NaN |
[] | 0 |
{} | NaN |
To String (via String())
| From | Result |
|---|
null | "null" |
undefined | "undefined" |
true | "true" |
42 | "42" |
[] | "" |
[1,2] | "1,2" |
{} | "[object Object]" |
Conversion functions
| Function | Behavior |
|---|
Number(x) | Strict — entire value must be numeric |
parseInt(s, radix) | Parses from start, stops at non-digit |
parseFloat(s) | Same, handles decimals |
+x (unary) | Same as Number(x) |
String(x) | Never throws |
Boolean(x) / !!x | Truthy → true, falsy → false |
Classic gotchas (one-liners)
"5" + 3
"5" - 3
1 + 2 + "3"
"1" + 2 + 3
[] + []
[] + {}
typeof null
NaN === NaN
[] == false
"" == false
"0" == false
"" == "0"
null == 0
null >= 0
Master decision guide
Need default value?
├─ 0, "", false are INVALID → use ||
└─ 0, "", false are VALID → use ??
Need type check?
├─ Primitive → typeof
├─ Array → Array.isArray()
├─ Null → === null
├─ Instance → instanceof
└─ Precise → Object.prototype.toString.call()
Need equality?
├─ Normal → ===
├─ null/undefined → == null
└─ NaN or -0 → Object.is()
End of 1.18 quick revision.