syntax Copy
string | number | boolean | undefined | null | symbol | bigintexample Copy
const name = "Mira" ;
const age = 28 ;
const active = true ;
const missing = undefined;
const empty = null ;
const id = Symbol("id" );
const big = 900719925474099267 n; Note Primitives are immutable and compared by value. There are 7 primitive types in total.
primitive types data types javascript types value types
syntax Copy
typeof valueexample Copy
console.log(typeof "hello" );
console.log(typeof 42 );
console.log(typeof true );
console.log(typeof undefined);
console.log(typeof null );
console.log(typeof []);
console.log(typeof (() => {})); 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.
check type typeof type checking determine type
syntax Copy
implicit: value + "" | +value | !!value
explicit: String(v) | Number(v) | Boolean(v)example Copy
console.log("5" + 3 );
console.log("5" - 3 );
console.log(+"42" );
console.log(!!"hello" );
console.log(Number("" ));
console.log(Number("ab" )); Note The + operator with a string always coerces to string. Prefer explicit conversion (Number(), String()) to avoid surprises.
type coercion implicit conversion convert type string to number truthy falsy
syntax Copy
value ?? fallbackexample Copy
const inputCount = 0 ;
console.log(inputCount || 10 );
console.log(inputCount ?? 10 );
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.
nullish coalescing null fallback default value null ?? operator null or undefined check
syntax Copy
obj?.prop
obj?.[expr]
obj?.method()example Copy
const user = { profile: { avatar: "pic.png" } };
console.log(user?.profile?.avatar);
console.log(user?.settings?.theme);
console.log(user?.getName?.()); Note Short-circuits to undefined when a link in the chain is null/undefined. Combine with ?? for defaults: user?.name ?? "Guest"
optional chaining safe property access null safe ?. operator avoid cannot read property
syntax Copy
variable ??= value;example Copy
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).
nullish assignment ??= operator assign if null logical assignment
syntax Copy
const sym = Symbol(description);example Copy
const STATUS = Symbol("status" );
const order = { [STATUS]: "shipped" , id: 101 };
console.log(order [STATUS]);
console.log(Object.keys(order )); Note Symbols are unique and hidden from normal enumeration. Use Symbol.for("key") to create shared/global symbols across modules.
symbol unique key hidden property symbol type
syntax Copy
false | 0 | -0 | 0 n | "" | null | undefined | NaNexample Copy
const values = [false , 0 , -0 , 0 n, "" , null , undefined, NaN];
const truthyOnes = values .filter(Boolean);
console.log(truthyOnes.length);output
0Note Everything else is truthy, including empty objects {}, empty arrays [], and the string "false". This trips up many developers.
falsy values truthy falsy boolean conversion which values are false
Equality: == vs === vs Object.is syntax Copy
a === b
a == b
Object.is(a, b) example Copy
console.log(0 == false );
console.log(0 === false );
console.log(NaN === NaN);
console.log(Object.is(NaN, NaN));
console.log(Object.is(0 , -0 )); Note Always use === for comparisons. Object.is() handles edge cases like NaN and -0 that even === gets wrong.
equality == vs === strict equality compare values Object.is