JavaScript Data Types Interview Questions and Answers

Compare JavaScript's primitive and reference values, coercion, and the behavior of less obvious types.

Frequently Asked Questions

Important questions are highlighted in the sections below; select one to jump to its answer.

Theoretical JavaScript Interview Questions

A data type tells JavaScript what kind of value you're working with. Is it a number? A piece of text? True or false? The type affects which operations and coercions apply. It does not expose a guaranteed memory layout. For example, numeric operators can coerce strings, while a number does not have string methods unless it is converted or boxed temporarily. You can't call .toUpperCase() on a number. Getting the data type wrong is one of the most common sources of bugs in JavaScript.

JavaScript has seven primitive data types. Primitive values are not objects and are immutable:

  • 1. Boolean
  • 2. Null
  • 3. Undefined
  • 4. Number
  • 5. BigInt (added in ES2020)
  • 6. String
  • 7. Symbol

All other values are objects. Arrays, dates, regular expressions, and functions are objects with different built-in behavior. "Reference type" is a useful teaching term, but JavaScript variables still receive values; object values can refer to the same object.

  • Object values: objects, arrays, functions, dates, and more

In JavaScript, a symbol is a primitive type introduced in ES6 that creates a guaranteed unique identifier. The main use case? Adding properties to objects without risking name collisions. Every Symbol() call creates a brand new, unique value - even Symbol("id") === Symbol("id") is false. You'll see them used in libraries and frameworks to add properties that are not exposed through ordinary string-key enumeration. Symbol properties are still discoverable with APIs such as Reflect.ownKeys().

javascript

const mySym = Symbol(8);

BigInt lets you work with integers beyond JavaScript's safe number limit (2^53 - 1). Regular numbers lose precision past that point, which can cause real bugs in financial or scientific apps. You create a BigInt by appending "n" to a number literal or calling the BigInt() constructor. One gotcha: you can't mix BigInt and regular numbers in the same expression without explicit conversion.

javascript

const bigNumber = BigInt(35445565654656);
const anotherBigNumber = 3454354543543543n;
Primitive Data Types Non-Primitive Data Types
Are primitive values with value semantics Are object values; assigning them can make two variables refer to the same object
Types: number, string, boolean, null, undefined, symbol, bigint Types: objects, arrays, functions, dates, regex
Immutable - you can't change the value itself, only reassign Mutable - you can change properties and contents in place
Storage location is an engine implementation detail Storage location is an engine implementation detail
Compared by their actual value Compared by reference - two identical objects are not equal unless they're the same object

Another famous JavaScript quirk: typeof null returns "object". This is actually a bug from the very first version of JavaScript in 1995, and it was never fixed because too much existing code depends on it. In reality, null is a primitive value that means "intentionally empty" or "no value." If you need to check for null specifically, use === null instead of typeof.

javascript

console.log(typeof null); //object

Every value in JavaScript has a hidden boolean personality. When you drop a value into an if statement, JavaScript secretly converts it to truthy or falsy. The falsy list is short and worth memorizing: false, 0, "" (empty string), null, undefined, and NaN. Everything else is truthy - including empty arrays and empty objects, which surprises a lot of people. So if ([]) is true, even though the array has nothing in it.

Manual type conversion is when you explicitly tell JavaScript to change a value's type, rather than letting the engine guess. You use built-in functions like Number(), String(), Boolean(), parseInt(), or parseFloat(). This makes your intent clear to anyone reading the code. For example, Number("42") gives you 42 as a number, and String(true) gives you "true". Always prefer this over relying on implicit coercion - your future self will thank you when debugging.

Declare a variable with constants using const and JavaScript won't let you reassign it. You must give it a value right away - no declaring now and assigning later. This is your go-to for values that shouldn't change, like API URLs, configuration values, or mathematical constants. A good rule of thumb: start with const for everything, and only switch to let when you actually need to reassign.

BigInt is for when regular numbers lose precision - anything beyond Number.MAX_SAFE_INTEGER (about 9 quadrillion) can silently give wrong results. You'll hit this with database IDs from certain systems, cryptocurrency values, or bitwise operations on large numbers. The catch: you can't mix BigInt with regular numbers in arithmetic - you'll get a TypeError - so you need to be explicit about conversions.

const big = 9007199254740991n + 1n;
console.log(big); // 9007199254740992n

// Cannot mix with regular numbers
console.log(10n + 5); // TypeError

var is the old way to declare variables - function-scoped and hoisted, which causes confusing bugs. let and const (ES6+) are block-scoped and much more predictable. Use const by default, and reach for let only when you actually need to reassign. Avoid var in modern code.

The big differences are scope and hoisting. var is function-scoped and hoisted as undefined - you can use it before its declaration and it won't throw. let and const are block-scoped and live in a 'temporal dead zone' until their declaration - accessing them early throws a ReferenceError. Also, const can't be reassigned, though the object it points to can still be mutated.

Default to const - it signals to readers that this value won't be reassigned, and the engine can potentially optimize it. Switch to let when you need to reassign (like a loop counter or accumulator). Never use var in new code - its function-scope and hoisting behavior produces bugs that are subtle and annoying to debug.

These three are different levels of 'nothing'. undefined means declared but not yet assigned - it's the default. null is an explicit empty value you set intentionally to say 'this has no value right now.' An undeclared variable was never declared at all, and trying to read it throws a ReferenceError. Use null to intentionally clear a value; rely on undefined checks to detect missing properties.

let a;
let b = null;

console.log(a); // undefined
console.log(b); // null
console.log(userName); // ReferenceError

Implicit coercion is JavaScript quietly converting types behind your back during operations - like '5' + 3 giving '53' instead of 8. It's one of the language's most notorious gotcha areas. The rules are complex: + with a string coerces to string, but - coerces to number. Use === instead of == to avoid comparison coercions, and convert types explicitly when you need control.

Explicit conversion means you're deliberately converting a value yourself - Number('42'), String(123), Boolean(0). This is always preferable to letting JavaScript coerce implicitly, because it makes your intent clear and avoids surprises. Number() is stricter than parseInt() - Number('42px') gives NaN, while parseInt('42px') gives 42.

The falsy values in JavaScript are: false, 0, 0n (BigInt zero), empty string ('', "", ``), null, undefined, and NaN. Everything else is truthy - including empty arrays and empty objects. The biggest gotcha is that [] and {} are truthy - if you want to check for an empty array, use arr.length === 0, not just if (!arr).

Regular numbers cap out at Number.MAX_SAFE_INTEGER (2^53 - 1) before they lose integer precision. BigInt handles arbitrarily large integers exactly, which matters for things like database IDs from 64-bit systems or financial calculations. The big limitation: you can't mix them in arithmetic - 10n + 5 throws a TypeError - you have to convert explicitly. Also, BigInt doesn't support decimal fractions.

A "normal copy" is just assigning one variable to another with =. For primitives it works as expected - you get an independent value. For objects and arrays you're just copying the reference, so both variables point to the same data in memory. This trips developers up constantly: they think they're working with a copy but they're actually mutating the original. It's the root cause of a whole class of subtle bugs.

A shallow copy creates a new object with the same top-level properties, but any nested objects or arrays are still shared references to the original. Use spread ({ ...obj } or [...arr]) or Object.assign() to make one. It's fine for flat objects, but if you shallow-copy { user: { name: 'Alice' } } and then change the name, you've changed it in both copies. For deeply nested structures, you need a deep copy.

A deep copy clones everything recursively - nested objects, nested arrays, all the way down - so modifying the copy never affects the original. The classic trick is JSON.parse(JSON.stringify(obj)), but it drops functions, converts Dates to strings, and can't handle circular references. The newer structuredClone() built-in is a much better option for most cases. For edge cases with functions or special types, use a library like Lodash's _.cloneDeep().

Objects are non-primitive values. Assigning an object value to another variable copies the value that refers to the same object, so both variables can observe a mutation. The language does not guarantee that objects live in a particular heap location or that primitives live on the stack. For example, const a = {}; const b = a; b.x = 1; makes a.x equal to 1 because a and b refer to the same object.

NaN is the special numeric value for an invalid or unrepresentable numeric result, and it remains part of JavaScript Number. Use Number.isNaN to test for it.

It is a historical JavaScript behavior retained for compatibility, not evidence that null is an object. Compare directly with null when checking for it.

Loose equality applies coercion rules that can produce surprising matches. Prefer === unless the intended coercion is explicit and well understood.

Each Symbol() call creates a unique primitive value, even with the same description, making symbols useful for collision-resistant property keys.

The types have distinct precision and conversion rules, so implicit mixing is disallowed. Convert explicitly when safe and ensure the Number is an integer within the required range.