Introduction & Syntax
Data Types · Study material
JavaScript has only a handful of types, but almost every tricky interview question depends on them: why 0.1 + 0.2 is not 0.3, why typeof null is "object", why NaN !== NaN, why [] === [] is false, and which values are falsy. This topic covers the seven primitive types and objects, how numbers really work, BigInt, null vs undefined, value vs reference, and converting between types. Then test yourself with 60 practice questions – 15 MCQs, 20 predict-the-output puzzles, 15 debugging questions and 10 coding problems – every one run on Node.js.
Primitive types and objects
| Type | Example | typeof |
|---|---|---|
| String | "Asha", 'x', `hi ${n}` | "string" |
| Number | 42, 3.14, NaN, Infinity | "number" |
| BigInt | 12345678901234567890n | "bigint" |
| Boolean | true, false | "boolean" |
| Undefined | undefined | "undefined" |
| Symbol | Symbol("id") | "symbol" |
| Null | null | "object" (historic bug) |
| Object | {}, [], new Date(), functions | "object" / "function" |
Primitives are immutable and compared by value. Everything else is an object: arrays, functions, dates, maps and plain objects are compared and copied by reference.
// The seven primitive types, then objects
console.log(typeof "LearnFrenzy"); // string
console.log(typeof 42, typeof 3.14); // number (one type for integers and decimals)
console.log(typeof 42n); // bigint
console.log(typeof true); // boolean
console.log(typeof undefined); // undefined
console.log(typeof Symbol("id")); // symbol
console.log(typeof null); // "object" – the famous exception
console.log(typeof {}, typeof [], typeof function () {});
string number number bigint boolean undefined symbol object object object function
Numbers: one floating-point type
JavaScript has no separate int and float: every number is a 64-bit IEEE 754 double. Decimals such as 0.1 cannot be stored exactly in binary, so arithmetic can show tiny errors. Whole numbers are exact only up to Number.MAX_SAFE_INTEGER (253 − 1). Invalid arithmetic gives NaN ("not a number", which is still of type number and is not equal even to itself); dividing a non-zero number by zero gives Infinity; and there is a negative zero that === cannot tell apart from 0 (use Object.is).
// Every number is a 64-bit floating-point value (IEEE 754 double)
console.log(0.1 + 0.2); // binary fractions are not exact
console.log(Number.MAX_SAFE_INTEGER); // 2^53 - 1
console.log(Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2);
console.log(typeof NaN, NaN === NaN, Number.isNaN(NaN)); // NaN is a number that equals nothing
console.log(1 / 0, -1 / 0, Object.is(-0, 0)); // Infinity and a signed zero
0.30000000000000004 9007199254740991 true number false true Infinity -Infinity false
BigInt
For whole numbers beyond 253 − 1 – IDs, cryptography, factorials – use BigInt: add n to a literal or call BigInt(). BigInt arithmetic is exact, division truncates, and BigInts cannot be mixed with numbers in arithmetic without an explicit conversion.
// BigInt: whole numbers of any size – write an n after the digits
const big = 9007199254740993n; // beyond MAX_SAFE_INTEGER, still exact
console.log(big + 2n);
console.log(typeof big, 10n / 3n); // BigInt division drops the fraction
try {
console.log(big + 1); // BigInt and Number cannot be mixed
} catch (e) {
console.log(e.name);
}
console.log(big + BigInt(1), Number(10n) + 1); // convert explicitly instead
9007199254740995n bigint 3n TypeError 9007199254740994n 11
null vs undefined
undefined means "no value has been assigned": an uninitialised variable, a missing property or argument, a function without return. null is a value a programmer assigns on purpose to mean "empty". They are loosely equal (null == undefined) but not strictly equal, and the ?? operator treats both as missing.
let notAssigned; // declared but never given a value
const user = { phone: null }; // null: "intentionally empty"
console.log(notAssigned, user.phone, user.email); // a missing property is undefined
console.log(typeof notAssigned, typeof user.phone);
console.log(null == undefined, null === undefined);
console.log(user.email ?? "no email", user.phone ?? "no phone"); // ?? catches both
undefined null undefined undefined object true false no email no phone
Truthy and falsy
When a value is used as a condition, it is converted to a boolean. There are exactly eight falsy values: false, 0, -0, 0n, "", null, undefined and NaN. Everything else is truthy – including "0", "false", " ", empty arrays and empty objects.
// Exactly eight falsy values – everything else is truthy
const falsy = [false, 0, -0, 0n, "", null, undefined, NaN];
console.log(falsy.length, falsy.every((v) => !v));
// Values that surprise people: all truthy
const truthy = ["0", "false", " ", [], {}, function () {}, -1, Infinity];
console.log(truthy.every((v) => Boolean(v)));
console.log(Boolean([]), Boolean(""), !!"0");
8 true true true false true
Value vs reference
Assigning a primitive copies the value. Assigning an object copies the reference, so both variables point to the same object and a change through one is visible through the other. === on objects checks whether they are the same object, not whether they look alike. Spread ({ ...obj }) makes a shallow copy; structuredClone() makes a deep one.
// Primitives are copied by value
let x = 10;
let y = x;
y++;
console.log(x, y);
// Objects are shared by reference: both names point to ONE object
const p = { score: 10 };
const q = p;
q.score++;
console.log(p.score, q.score, p === q);
// A spread copy is a new object (but only one level deep)
const r = { ...p };
r.score = 99;
console.log(p.score, r.score, p === r);
console.log({ a: 1 } === { a: 1 }); // two different objects
10 11 11 11 true 11 99 false false
Converting between types
Number(x) converts the whole value (empty string → 0, invalid → NaN, null → 0, undefined → NaN). parseInt and parseFloat read digits from the start and stop at the first invalid character – always pass the radix to parseInt. String(x) or x.toString(radix) convert to text, and Boolean(x) or !!x to a boolean. Implicit conversion by operators such as + and == is covered in the Operators topic.
// To number: Number() needs the WHOLE string to be numeric; parseInt/parseFloat read a prefix
console.log(Number("42"), Number(""), Number("12px"), Number(null), Number(undefined));
console.log(parseInt("12px"), parseFloat("3.5kg"), parseInt("0x1F"), parseInt("101", 2));
// To string and to boolean
console.log(String(123) + 1, (255).toString(16), Boolean("0"), Boolean(""));
42 0 NaN 0 NaN 12 3.5 31 5 1231 ff true false
Strings and symbols
Strings are immutable: s[0] = "H" changes nothing (and throws in strict mode); methods like toUpperCase() return a new string. Calling a method on a primitive works because the engine wraps it in a temporary object (autoboxing) – never create wrappers yourself with new String() or new Number(). A Symbol is a unique value, often used as a property key that cannot clash with other keys.
// Strings are immutable primitives: methods return NEW strings
const s = "hello";
console.log(s.toUpperCase(), s);
// "hello".length works because the engine wraps the primitive in a temporary String object
console.log(s.length, typeof s, typeof new String("hi"));
// Symbols: every Symbol() is unique – useful as hidden, collision-free property keys
const id1 = Symbol("id");
const id2 = Symbol("id");
console.log(id1 === id2, id1.toString(), id1.description);
HELLO hello 5 string object false Symbol(id) id
Common mistakes
- Comparing decimals with === – 0.1 + 0.2 !== 0.3; compare with a tolerance or work in integers.
- typeof x === "object" to find objects – it is also true for null and arrays.
- x === NaN – always false; use Number.isNaN(x).
- Global isNaN("") is false – it converts first; "" becomes 0.
- Mixing BigInt and Number – a TypeError; convert one side explicitly.
- Copying an object with = – both names share one object.
- Treating "0" or [] as false – both are truthy.
Key points to remember
- 7 primitives: string, number, bigint, boolean, undefined, symbol, null – plus objects.
- typeof null is "object"; typeof a function is "function"; use Array.isArray for arrays.
- All numbers are doubles: exact integers only up to 2^53 − 1.
- NaN is a number, not equal to itself; check with Number.isNaN.
- 8 falsy values: false, 0, -0, 0n, "", null, undefined, NaN.
- Primitives are copied by value, objects by reference.
- Number() converts the whole string; parseInt reads a prefix.
Frequently asked questions
Why does typeof null return "object"?
In the first JavaScript engine, values carried a type tag and null used the tag for objects. Fixing it would break existing websites, so the behaviour is part of the standard.
Is everything in JavaScript an object?
No. Primitives are not objects – they only behave like objects when you call a method, because the engine wraps them in a temporary wrapper.
When should I use BigInt?
For integers that may exceed 2^53 − 1 and must stay exact: large IDs, financial calculations in the smallest unit at huge scale, combinatorics. For ordinary counting and decimals, Number is faster and simpler.
Where can I learn more?
Read JavaScript Data Types and Type Coercion in the JavaScript tutorial, then practise with the exercises below.