JavaScript Data Types

Learn what kind of data JavaScript can store and how types behave in real code.

Primitive Types Reference Types Interview + MCQ

Table of Contents

What are Data Types in JavaScript?

Data type defines the kind of value a variable stores. Choosing the right type helps avoid bugs and improves readability. If you have not reviewed declarations yet, start with JavaScript Variables first.

JavaScript data types chart showing primitive and reference types with examples
Overview of JavaScript primitive and reference data types.
let name = "Nikhil";      // string
let age = 25;            // number
let isActive = true;     // boolean
let score = null;        // null
let city;                // undefined

Primitive Types

Primitive types are immutable and stored by value. JavaScript primitives are: string, number, bigint, boolean, undefined, null, and symbol.

TypeExampleUse Case
string"hello"Text values, labels
number42, 3.14Math, counts, amounts
bigint9007199254740993nVery large integers
booleantrue, falseConditions, flags
undefinedlet x;Declared but not assigned
nullnullIntentional empty value
symbolSymbol("id")Unique property keys

Symbol (ES6+)

Symbol creates unique, immutable identifiers. Even symbols with the same description are different values.

// Creating Symbols
let sym1 = Symbol();
let sym2 = Symbol("description");
let sym3 = Symbol("description");

console.log(sym2 === sym3);  // false (always unique!)
console.log(sym2.description);  // "description"

// Symbols as object keys (hidden from normal iteration)
let id = Symbol("id");
let user = {
    name: "John",
    [id]: 12345  // Symbol as key
};

console.log(user[id]);     // 12345
console.log(Object.keys(user));  // ["name"] (symbol not shown)
console.log(Object.getOwnPropertySymbols(user)); // [Symbol(id)]

// Well-known symbols
const myArray = [1, 2, 3];
const iterator = myArray[Symbol.iterator]();
console.log(iterator.next());  // { value: 1, done: false }

// Use cases: preventing property name collisions
const WATER = Symbol("water");
const COFFEE = Symbol("coffee");

let drink = {
    [WATER]: "H2O",
    [COFFEE]: "Caffeine"
};

Reference (Non-Primitive) Types

Objects, arrays, and functions are reference types. They are stored by reference, so changes through one variable can affect another linked variable.

Non-Primitive (Reference) Data Types

Non-primitives are mutable and stored by reference in memory.

Data TypeDescriptionMutabilityStored By
ObjectKey-value pairsMutableReference
ArrayOrdered listMutableReference
FunctionCallable objectMutableReference
DateDate/timeMutableReference
RegExpRegular expressionMutableReference
Map/SetES6 collectionsMutableReference
const user = { name: "Asha" };
const copy = user;
copy.name = "Ravi";

console.log(user.name); // "Ravi"
// Both variables point to same object reference

This behavior is important when you later learn Objects and Arrays.

Primitive vs Reference: Key Differences

// PRIMITIVES - Stored by VALUE
let a = 10;
let b = a;      // b gets a COPY of the value
b = 20;
console.log(a); // 10 (unchanged)
console.log(b); // 20

// REFERENCE TYPES - Stored by REFERENCE
let obj1 = { value: 10 };
let obj2 = obj1;    // obj2 gets REFERENCE to same object
obj2.value = 20;
console.log(obj1.value); // 20 (changed!)
console.log(obj2.value); // 20

// Proving they reference the same object
console.log(obj1 === obj2);  // true (same reference)

// Creating a true copy (shallow)
let obj3 = { ...obj1 };      // Spread operator
let obj4 = Object.assign({}, obj1);
console.log(obj1 === obj3);  // false (different objects)

// Deep copy for nested objects
let nested = { a: { b: 1 } };
let deepCopy = JSON.parse(JSON.stringify(nested));

Visual Memory Representation

PRIMITIVE (by VALUE):
┌─────────────────────────────────────────┐
│ STACK Memory                            │
├────────────┬────────────────────────────┤
│ Variable   │ Value                      │
├────────────┼────────────────────────────┤
│ a          │ 10                         │
│ b          │ 20 (separate copy)         │
└────────────┴────────────────────────────┘

REFERENCE (by REFERENCE):
┌─────────────────────────────────────────┐
│ STACK           │ HEAP                  │
├────────────┬────┼───────────────────────┤
│ Variable   │ Ref│ Actual Object         │
├────────────┼────┼───────────────────────┤
│ obj1       │ 0x1│ 0x1: { value: 20 }    │
│ obj2       │ 0x1│ (same reference!)     │
└────────────┴────┴───────────────────────┘

Type Checking

Methods to Check Data Types

// 1. typeof operator (best for primitives)
console.log(typeof "hello");    // "string"
console.log(typeof 42);         // "number"
console.log(typeof true);       // "boolean"
console.log(typeof undefined);  // "undefined"
console.log(typeof Symbol());   // "symbol"
console.log(typeof 42n);        // "bigint"
console.log(typeof function(){}); // "function"
console.log(typeof {});         // "object"
console.log(typeof null);       // "object" (BUG in JS!)

// 2. instanceof (for objects)
console.log([] instanceof Array);        // true
console.log({} instanceof Object);       // true
console.log(new Date() instanceof Date); // true
console.log([] instanceof Object);       // true (arrays are objects)

// 3. Array.isArray() (best for arrays)
console.log(Array.isArray([]));          // true
console.log(Array.isArray({}));          // false

// 4. isNaN() vs Number.isNaN()
console.log(isNaN("hello"));   // true (coerces to NaN)
console.log(Number.isNaN("hello")); // false (no coercion)
console.log(Number.isNaN(NaN)); // true

// 5. constructor property
console.log((42).constructor.name);     // "Number"
console.log(("hi").constructor.name);   // "String"
console.log(([]).constructor.name);     // "Array"
console.log(({}).constructor.name);     // "Object"

// 6. Custom type checking function
function getType(value) {
    if (value === null) return "null";
    if (value === undefined) return "undefined";
    if (Array.isArray(value)) return "array";
    return typeof value;
}

console.log(getType(null));   // "null"
console.log(getType([]));     // "array"
console.log(getType({}));     // "object"

typeof, null vs undefined

console.log(typeof "hello");   // "string"
console.log(typeof 10);        // "number"
console.log(typeof true);      // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null);      // "object" (historical JS behavior)
  • undefined: variable exists but no value assigned yet.
  • null: intentional assignment to represent empty/no object.

Type Conversion (Explicit + Implicit)

JavaScript can convert values automatically (implicit coercion) or manually using conversion functions (explicit conversion).

// Explicit conversion
let n = Number("123");   // 123
let s = String(456);     // "456"
let b = Boolean(1);      // true

// Implicit conversion
console.log("5" + 2);    // "52"
console.log("5" - 2);    // 3
console.log(true + 1);   // 2

For clean logic and fewer bugs, prefer explicit conversion in most production code.

Common Pitfalls

// 1. Forgetting null is an object
let data = null;
if (typeof data === "object") {  // This will be true!
    // Runs incorrectly
}

// Better null check
if (data === null) { }

// 2. NaN is not equal to itself
console.log(NaN === NaN);  // false!
// Use isNaN() or Number.isNaN()
if (isNaN(value)) { }

// 3. Floating point precision
console.log(0.1 + 0.2 === 0.3);  // false
// Use tolerance or toFixed()
let tolerance = 0.000001;
console.log(Math.abs(0.1 + 0.2 - 0.3) < tolerance);  // true

// 4. Using == instead of ===
console.log(0 == false);   // true (coercion!)
console.log("" == false);  // true
console.log(0 === false);  // false (use ===)

// 5. Mutating objects unintentionally
let original = { name: "John" };
let copy = original;
copy.name = "Jane";
console.log(original.name);  // "Jane" (changed!)
// Create true copy
let safeCopy = { ...original };

Best Practices

  • Use strict equality (===) to avoid unintended type coercion.
  • Initialize variables with meaningful default values.
  • Avoid mixing number and string operations unknowingly.
  • Use Array.isArray(value) to check arrays reliably.
  • Link your learning with Operators because type behavior heavily affects expressions.

10 JavaScript Data Types Interview Q&A

1What are JavaScript primitive data types?easy
Answer: string, number, bigint, boolean, undefined, null, symbol.
2Why is typeof null equal to "object"?medium
Answer: It is a historical language bug kept for backward compatibility.
3Difference between primitive and reference types?easy
Answer: Primitive values copy by value; reference types copy by reference address.
4When to use bigint?medium
Answer: For integers larger than Number safe range (2^53 - 1).
5How to check if a value is array?easy
Answer: Use Array.isArray(value).
6Difference between null and undefined?easy
Answer: undefined means not assigned; null means intentionally empty.
7What is type coercion?medium
Answer: Automatic conversion of one type to another during operations/comparisons.
8How do you convert string to number safely?medium
Answer: Use Number() and validate with Number.isNaN().
9What is Symbol data type for?hard
Answer: To create unique identifiers, often used for object property keys.
10Why use strict equality operator?easy
Answer: It avoids implicit coercion and gives more predictable comparisons.

10 JavaScript Data Types MCQs

1

Which is NOT a primitive type?

Astring
Bnumber
Cobject
Dboolean
Explanation: object is reference type, not primitive.
2

Result of typeof null is:

Anull
Bobject
Cundefined
Dsymbol
Explanation: Historical behavior returns "object".
3

Which method reliably checks arrays?

Atypeof arr
Barr instanceof Object only
CArray.isArray(arr)
Darr.type
Explanation: Use Array.isArray() for array checks.
4

Which represents intentionally empty value?

Aundefined
Bnull
Cfalse
DNaN
Explanation: null is intentional absence of value.
5

"5" + 2 returns:

A7
B52
CNaN
DError
Explanation: + with string causes concatenation.
6

Which type handles very large integers?

Anumber
Bbigint
Csymbol
Dobject
Explanation: bigint stores large integers precisely.
7

Value of uninitialized variable is:

Anull
B0
Cundefined
Dfalse
Explanation: Declared-but-not-assigned variable is undefined.
8

Strict equality avoids:

AHoisting
BType coercion surprises
CAll runtime errors
DReference types
Explanation: === compares value + type without coercion.
9

Which conversion returns false?

ABoolean("hello")
BBoolean(1)
CBoolean(0)
DBoolean([])
Explanation: 0 is falsy, so Boolean(0) is false.
10

What does Symbol() primarily provide?

ALarge numbers
BUnique identifiers
CAsync flow
DJSON parsing
Explanation: Symbol creates unique values useful for object keys.