HuntExams Academy logo
JavaScriptBeginner#fundamentals#types

What are the data types in JavaScript?

Answer

Seven primitives — string, number, bigint, boolean, undefined, symbol, null — plus the object type (which covers arrays, functions, dates and plain objects). Primitives are immutable and copied by value; objects are copied by reference.

Example

typeof 'hi';        // 'string'
typeof 10n;         // 'bigint'
typeof null;        // 'object' (historic bug)
typeof [];          // 'object'
typeof function(){} // 'function'

Detailed explanation

JavaScript has eight language-level data types: seven primitive types—string, number, bigint, boolean, undefined, symbol and null—and the object type. JavaScript is dynamically typed, meaning values have types but variables are not restricted to one type. For example, let value = 10; followed by value = 'ten'; is valid. Knowing these types helps you predict comparisons, conversions, function behaviour and unexpected results when handling forms, API responses or missing data.

A string represents text, while a boolean is either true or false. The number type uses IEEE 754 double-precision floating-point representation for both integers and fractional values; NaN and Infinity also belong to this type. Integers are safely represented between -(2 ** 53 - 1) and 2 ** 53 - 1. BigInt represents integers of arbitrary size, subject to available resources, and uses literals such as 10n. Undefined usually indicates an absent or unassigned value, whereas null commonly expresses intentional absence. Symbol creates unique primitive identifiers, often used as property keys; two calls to Symbol('id') produce different values.

Objects include plain objects, arrays, functions and dates. Unlike primitives, objects have identity and can generally be mutated, unless restrictions such as freezing apply. Primitive values themselves are immutable, although a variable holding one can be reassigned. Assignment and argument passing always copy a value. For objects, that value is a reference, so two variables can point to the same object and observe its mutations. This is more precise than saying JavaScript passes objects by reference: reassigning a function parameter does not reassign the caller's variable.

The typeof operator helps inspect values: typeof 'hi' returns 'string', typeof 10n returns 'bigint', and typeof undefined returns 'undefined'. Important exceptions are typeof null returning 'object', a historical quirk, and functions returning 'function' despite belonging to the object type. Arrays return 'object', so use Array.isArray(value) to identify them and value === null to check for null. Interviewers typically expect the complete list, these exceptions and a clear distinction between primitive immutability and shared object identity.

Key points to remember

  • The seven primitives are string, number, bigint, boolean, undefined, symbol and null.
  • Arrays, functions, dates and plain objects all belong to the object type.
  • JavaScript is dynamically typed: a variable can hold values of different types over time.
  • Primitives are immutable; copied object references share the same object's identity.
  • typeof null is 'object', typeof a function is 'function', and Array.isArray() detects arrays.

Common mistakes candidates make

  • Calling null an object because typeof null returns 'object'; null is a primitive.
  • Saying objects are passed by reference without explaining that the reference value is copied.
  • Treating arrays as a separate language-level type or assuming typeof can distinguish them from plain objects.

Likely follow-up questions

What is the difference between null and undefined?

Undefined commonly appears when a variable has no assigned value, a property is missing or a function returns no value. Null is typically assigned explicitly to indicate intentional absence; null === undefined is false.

What happens when you copy an object into another variable?

With const a = { count: 1 }; const b = a;, both variables refer to the same object, so b.count = 2 also changes what a.count reads. Reassigning a variable instead changes only that variable's reference, provided reassignment is allowed.

How are number and bigint different?

Number supports floating-point values but cannot represent every integer beyond its safe-integer range exactly, while BigInt represents integers without that precision limitation. Most arithmetic operators cannot mix them directly: 1n + 1 throws a TypeError, so an explicit conversion is needed.

Interview tip: Start with the seven primitives and object, then demonstrate the typeof exceptions and one shared-object mutation example.

Related Interview Questions

1
JavaScriptBeginner#scope#es6

Difference between var, let and const?

Open
2
JavaScriptBeginner#scope

What is hoisting?

Open
3
JavaScriptIntermediate#closures#functions

What is a closure?

Open