Difficulty: Beginner
What are the data types in JavaScript, and how do you reliably check the type of a value?
JavaScript has eight data types: seven primitives (string, number, bigint, boolean, undefined, null, symbol) and object, which covers arrays, functions, dates, maps and everything else. Primitives are immutable and compared by value, while objects are compared by reference. For type checks, use typeof for primitives and functions, Array.isArray for arrays, === null for null, and instanceof or Object.prototype.toString for specific object kinds.
string: UTF-16 text, immutable. Methods return new strings.number: a 64-bit IEEE 754 float. There is no separate integer type, which is why 0.1 + 0.2 !== 0.3 and why integers are only exact up to Number.MAX_SAFE_INTEGER (2^53 - 1). Special values include NaN, Infinity and -0.bigint: arbitrary-precision integers written as 10n. You cannot mix bigint and number in arithmetic without converting.boolean: true or false.undefined: the value of a declared but unassigned variable, a missing property or a missing argument.null: an intentional "no value".symbol: a unique identifier, often used as non-colliding object keys (Symbol.iterator).Anything that is not a primitive is an object. Functions are callable objects, arrays are objects with integer keys and a length, and Date, Map, Set, RegExp and Promise are all objects.
When you assign or pass a primitive, the value is copied. When you assign or pass an object, the reference is copied, so both names point at the same object. That is why mutating an array inside a function changes the caller's array, and why {} === {} is false: two different objects never compare equal, even with identical contents.
| Value | typeof result |
|---|---|
'hi' | "string" |
42, NaN | "number" |
10n | "bigint" |
true | "boolean" |
undefined | "undefined" |
Symbol() | "symbol" |
null | "object" (a historical bug that can never be fixed) |
[], {}, new Date() | "object" |
function () {}, classes | "function" |
So typeof alone cannot tell an array from an object, or null from an object.
value === null. For null or undefined together, value == null is the one accepted use of loose equality.Array.isArray(value). It works across iframes and realms, where instanceof Array fails because each realm has its own Array constructor.Number.isNaN(value), not the global isNaN, which coerces strings first (isNaN('abc') is true).Number.isInteger and Number.isSafeInteger.value instanceof Date, which walks the prototype chain.Object.prototype.toString.call(value) returns strings like [object Date] or [object Null].Money is a classic example. Storing a rupee amount like 199.99 as a float invites rounding errors, so payment systems such as Razorpay's APIs take amounts as integers in paise (19999). Large IDs from databases (64-bit integers) can exceed the safe integer range and silently lose precision when parsed as number; send them as strings or parse them as BigInt.
console.log(typeof 'text'); // string
console.log(typeof 3.14); // number
console.log(typeof NaN); // number
console.log(typeof 9007199254740993n); // bigint
console.log(typeof Symbol('id')); // symbol
console.log(typeof undefined); // undefined
console.log(typeof null); // object (legacy bug)
console.log(typeof []); // object
console.log(typeof class A {}); // function
typeof is perfect for primitives and functions, but reports object for null, arrays and every other object type.
function getType(value) {
if (value === null) return 'null';
if (Array.isArray(value)) return 'array';
const t = typeof value;
if (t !== 'object') return t;
// e.g. "[object Date]" -> "date"
return Object.prototype.toString.call(value).slice(8, -1).toLowerCase();
}
console.log(getType(null)); // null
console.log(getType([1, 2])); // array
console.log(getType(new Date())); // date
console.log(getType(new Map())); // map
console.log(getType(/upi/)); // regexp
console.log(getType(42)); // number
Handle null and arrays explicitly, use typeof for primitives, and fall back to the internal tag from Object.prototype.toString for specific object kinds.
let a = 10;
let b = a;
b++;
console.log(a, b); // 10 11
const cart1 = ['milk'];
const cart2 = cart1; // same array
cart2.push('bread');
console.log(cart1); // [ 'milk', 'bread' ]
console.log([1] === [1]); // false: different objects
console.log(Number.MAX_SAFE_INTEGER + 2); // 9007199254740992 (precision lost)
Primitives are copied by value, objects share a reference. The last line shows why large integers need BigInt or strings.
Primitive Types, Reference Types, typeof, instanceof, Array.isArray, BigInt