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:
- Primitive Data Types (immutable)
- Non-Primitive (Reference) Data Types (mutable)
Ques: What is Primitive Data Types?
Primitive types are stored by value, not by reference.
| Type | Example | Description |
|---|---|---|
| String | "Hello" | Sequence of characters |
| Number | 42, 3.14 | Integer or floating-point |
| BigInt | 123n | For very large integers |
| Boolean | true, false | Logical values |
| Undefined | let a; | Variable declared but not assigned |
| Null | let x = null; | Intentional empty value |
| Symbol | Symbol("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).
| Type | Example | Description |
|---|---|---|
| Object | { name: "John", age: 30 } | Collection of key–value pairs |
| Array | [1, 2, 3] | Ordered list of values |
| Function | function greet(){} | Reusable block of code |
| Date | new Date() | Date and time object |
| RegExp | /abc/ | Regular expressions for pattern matching |
Ques: What is the difference between Primitive and Non-Primitive data types?
| Feature | Primitive | Non-Primitive |
|---|---|---|
| Stored as | Value | Reference (memory address) |
| Mutable | Immutable | Mutable |
| Example | Number, String, Boolean | Object, Array, Function |
| Comparison | Compared by value | Compared 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
| Feature | Null | Undefined |
|---|---|---|
| Meaning | Intentional empty value | No value assigned |
| Type | Object | Undefined |
| Example | let 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:
- Implicit Conversion (Type Coercion) – Done automatically by JS
- 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?
NaNstands 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 typeinstanceof→ checks if an object is an instance of a class
Example:
[] instanceof Array; // true
{} instanceof Object; // true
