JS

Data Types

JavaScript · 9 entries

Primitive Types

syntax
string | number | boolean | undefined | null | symbol | bigint
example
const name = "Mira";       // string
const age = 28;            // number
const active = true;       // boolean
const missing = undefined; // undefined
const empty = null;        // null
const id = Symbol("id");   // symbol
const big = 900719925474099267n; // bigint

Note Primitives are immutable and compared by value. There are 7 primitive types in total.

typeof Operator

syntax
typeof value
example
console.log(typeof "hello");    // "string"
console.log(typeof 42);         // "number"
console.log(typeof true);       // "boolean"
console.log(typeof undefined);  // "undefined"
console.log(typeof null);       // "object" (!)  
console.log(typeof []);         // "object"
console.log(typeof (() => {})); // "function"

Note typeof null === "object" is a historic bug that will never be fixed. Use value === null for null checks. Arrays report as "object" -- use Array.isArray() instead.

Type Coercion

syntax
implicit: value + "" | +value | !!value
explicit: String(v) | Number(v) | Boolean(v)
example
console.log("5" + 3);    // "53" (string concat)
console.log("5" - 3);    // 2   (numeric)
console.log(+"42");       // 42  (to number)
console.log(!!"hello");   // true (to boolean)
console.log(Number(""));  // 0
console.log(Number("ab")); // NaN

Note The + operator with a string always coerces to string. Prefer explicit conversion (Number(), String()) to avoid surprises.

Nullish Coalescing (??)

syntax
value ?? fallback
example
const inputCount = 0;
console.log(inputCount || 10);  // 10 (wrong!)
console.log(inputCount ?? 10);  // 0  (correct)

const label = null;
console.log(label ?? "Untitled");
output
10
0
"Untitled"

Note Only triggers on null/undefined, unlike || which triggers on all falsy values (0, "", false, NaN). Use ?? when 0 or empty string are valid values.

Optional Chaining (?.)

syntax
obj?.prop
obj?.[expr]
obj?.method()
example
const user = { profile: { avatar: "pic.png" } };
console.log(user?.profile?.avatar);  // "pic.png"
console.log(user?.settings?.theme);  // undefined
console.log(user?.getName?.());      // undefined (no error)

Note Short-circuits to undefined when a link in the chain is null/undefined. Combine with ?? for defaults: user?.name ?? "Guest"

Nullish Assignment (??=)

syntax
variable ??= value;
example
let config = { timeout: null, retries: 3 };
config.timeout ??= 5000;
config.retries ??= 10;
console.log(config);
output
{ timeout: 5000, retries: 3 }

Note Only assigns when the left side is null or undefined. Also available: ||= (assigns on falsy) and &&= (assigns on truthy).

Symbols

syntax
const sym = Symbol(description);
example
const STATUS = Symbol("status");
const order = { [STATUS]: "shipped", id: 101 };

console.log(order[STATUS]);       // "shipped"
console.log(Object.keys(order));  // ["id"]

Note Symbols are unique and hidden from normal enumeration. Use Symbol.for("key") to create shared/global symbols across modules.

Falsy Values

syntax
false | 0 | -0 | 0n | "" | null | undefined | NaN
example
const values = [false, 0, -0, 0n, "", null, undefined, NaN];
const truthyOnes = values.filter(Boolean);
console.log(truthyOnes.length);
output
0

Note Everything else is truthy, including empty objects {}, empty arrays [], and the string "false". This trips up many developers.

Equality: == vs === vs Object.is

syntax
a === b   // strict (no coercion)
a == b    // loose (with coercion)
Object.is(a, b)  // same-value equality
example
console.log(0 == false);         // true
console.log(0 === false);        // false
console.log(NaN === NaN);        // false
console.log(Object.is(NaN, NaN)); // true
console.log(Object.is(0, -0));    // false

Note Always use === for comparisons. Object.is() handles edge cases like NaN and -0 that even === gets wrong.