Episode 1 — Fundamentals / 1.18 — Operators and Type System

1.18 — Operators and the Type System: Quick Revision

Compact cheat sheet. Print-friendly.

How to use this material (instructions)

  1. Skim before labs or interviews.
  2. Drill gaps — reopen README.md1.18.a1.18.g.
  3. Practice1.18-Exercise-Questions.md.
  4. Polish answers1.18-Interview-Questions.md.

Arithmetic operators

OperatorNameExampleResult
+Addition / concat5 + 3 / "5" + 38 / "53"
-Subtraction"10" - 37
*Multiplication4 * 312
/Division7 / 23.5
%Remainder10 % 31
**Exponentiation2 ** 38
++xPre-incrementReturns newx = x + 1 first
x++Post-incrementReturns oldx = x + 1 after

+ rule: If either operand is a string, concatenate. Other operators always convert to number.


Special numeric values

ValueWhen it appearsCheck
NaNInvalid math (0/0, "hello" * 2)Number.isNaN(x)
Infinity1/0Number.isFinite(x)
-Infinity-1/0Number.isFinite(x)

typeof NaN === "number" and NaN !== NaN.


Comparison operators

OperatorNameCoerces?
===Strict equalityNo (default choice)
!==Strict inequalityNo
==Loose equalityYes
!=Loose inequalityYes
> < >= <=RelationalYes (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

OperatorReturnsShort-circuits when
&&First falsy or last valueLeft is falsy
||First truthy or last valueLeft is truthy
!Inverted boolean
??Right if left is null/undefinedLeft is NOT null/undefined

Precedence: ! > && > || > ??

Cannot mix ?? with &&/|| without parentheses.


|| vs ?? quick comparison

Expression||??
0 op 42420
"" op "default""default"""
false op truetruefalse
null op "fallback""fallback""fallback"
undefined op "fallback""fallback""fallback"

Rule: Use ?? when 0, "", or false are valid values.


Assignment operators

OperatorMeaning
=Basic assignment
+= -= *= /= %= **=Compound (operate + assign)
||=Assign if current is falsy
&&=Assign if current is truthy
??=Assign if current is null/undefined

typeof results

Valuetypeof
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
ArrayArray.isArray(x)
Nullx === null
Null or undefinedx == null
Real object (not null, not array)x !== null && typeof x === "object" && !Array.isArray(x)
Number (not NaN)typeof x === "number" && !Number.isNaN(x)
Functiontypeof x === "function"
Any type preciselyObject.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

FromResult
"" / " "0
"42"42
"42px"NaN
true / false1 / 0
null0
undefinedNaN
[]0
{}NaN

To String (via String())

FromResult
null"null"
undefined"undefined"
true"true"
42"42"
[]""
[1,2]"1,2"
{}"[object Object]"

Conversion functions

FunctionBehavior
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) / !!xTruthy → true, falsy → false

Classic gotchas (one-liners)

"5" + 3        // "53"   (string wins with +)
"5" - 3        // 2      (numeric with -)
1 + 2 + "3"   // "33"   (left-to-right)
"1" + 2 + 3   // "123"  (concat after first string)
[] + []        // ""
[] + {}        // "[object Object]"
typeof null    // "object"
NaN === NaN    // false
[] == false    // true   (but [] is truthy)
"" == false    // true
"0" == false   // true
"" == "0"      // false
null == 0      // false
null >= 0      // true

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.