J

JavaScript Handbook

Clean • Professional

JavaScript Data Types — Top Interview Questions & Answers

4 minute

JavaScript Data Types — Interview Questions & Answers

Ques: What are Data Types in JavaScript?

Ans: Data types define the kind of value a variable can hold.

JavaScript is a dynamically typed language — the type is determined automatically at runtime.

Example:

let name = "John";   // string
let age = 25;        // number
let isAdmin = true;  // boolean

Ques: How many data types are there in JavaScript?

Ans: JavaScript has two categories of data types:

  1. Primitive Data Types (immutable)
  2. Non-Primitive (Reference) Data Types (mutable)

Ques: What is Primitive Data Types?

Primitive types are stored by value, not by reference.

TypeExampleDescription
String"Hello"Sequence of characters
Number42, 3.14Integer or floating-point
BigInt123nFor very large integers
Booleantrue, falseLogical values
Undefinedlet a;Variable declared but not assigned
Nulllet x = null;Intentional empty value
SymbolSymbol("id")Unique and immutable identifier

Example:

let str = "ChatGPT";    // String
let num = 2025;         // Number
let big = 12345678901234567890n; // BigInt
let flag = false;       // Boolean
let temp;               // Undefined
let empty = null;       // Null
let sym = Symbol("key"); // Symbol

Ques: What is Non-Primitive (Reference) Data Types?

Non-primitive values are stored by reference (point to memory locations).

TypeExampleDescription
Object{ name: "John", age: 30 }Collection of key–value pairs
Array[1, 2, 3]Ordered list of values
Functionfunction greet(){}Reusable block of code
Datenew Date()Date and time object
RegExp/abc/Regular expressions for pattern matching

Ques: What is the difference between Primitive and Non-Primitive data types?

FeaturePrimitiveNon-Primitive
Stored asValueReference (memory address)
MutableImmutableMutable
ExampleNumber, String, BooleanObject, Array, Function
ComparisonCompared by valueCompared by reference

Example:

let a = 10;
let b = a;
b = 20;
console.log(a); // 10 (independent copy)

let obj1 = {name: "John"};
let obj2 = obj1;
obj2.name = "Mike";
console.log(obj1.name); // "Mike" (reference shared)

Ques: What is null in JavaScript?

Ans: null represents an intentional empty or unknown value.

Example:

let car = null; // explicitly means "no value"

Ques: What is undefined in JavaScript?

Ans: A variable that has been declared but not assigned any value is undefined.

Example:

let name;
console.log(name); // undefined

Difference Between Null and Undefined

FeatureNullUndefined
MeaningIntentional empty valueNo value assigned
TypeObjectUndefined
Examplelet a = null;let b;

Ques: How can you check the data type of a variable?

Ans: Using the typeof operator.

Example:

console.log(typeof 42);       // "number"
console.log(typeof "hello");  // "string"
console.log(typeof null);     // "object"
console.log(typeof []);       // "object"

Ques: What is Type Conversion in JavaScript?

Ans: It’s the process of changing data from one type to another.

There are two types:

  1. Implicit Conversion (Type Coercion) – Done automatically by JS
  2. Explicit Conversion – Done manually by the developer

Example (Implicit):

console.log("5" + 3); // "53" (string)
console.log("5" - 2); // 3   (number)

Example (Explicit):

Number("42");  // 42
String(42);    // "42"
Boolean(1);    // true

Ques: What is Type Coercion?

Ans: JavaScript automatically converts data types when performing operations between mismatched types.

Example:

console.log(1 + "2"); // "12"
console.log("5" * 2); // 10

Ques: Why is typeof null “object”?

Ans: It’s a historical bug in JavaScript.

null was incorrectly set as type "object" in the early implementation of JS, and it remains for backward compatibility.

Ques: What is NaN in JavaScript?

  • NaN stands for Not a Number.
  • It represents an invalid numeric result.

Example:

console.log(0 / 0); // NaN
console.log(parseInt("abc")); // NaN
console.log(typeof NaN); // number

Ques: How to check if a value is NaN?

Ans: Use isNaN() or Number.isNaN().

Example:

isNaN("Hello"); // true
Number.isNaN("Hello"); // false (stricter check)

Ques: What are BigInt and when should you use it?

Ans: BigInt allows representing numbers larger than 2^53 - 1 (the limit for Number type).

Example:

let big = 9007199254740991n;
console.log(typeof big); // bigint

Ques: Can you mix BigInt and Number types?

Ans: No, mixing BigInt and Number in the same operation throws an error.

Example:

let x = 10n;
let y = 5;
// console.log(x + y); // TypeError

Ques: What’s the difference between typeof and instanceof?

  • typeof → checks data type
  • instanceof → checks if an object is an instance of a class

Example:

[] instanceof Array; // true
{} instanceof Object; // true

 

Article 0 of 0