JavaScript Interview Questions – Data Types

1. [Amazon] What are the different data types in JavaScript?

Answer:
JavaScript has two categories of data types:

  1. Primitive Data Types (Immutable, stored by value):
    • String โ†’ "Hello"
    • Number โ†’ 42, 3.14
    • Boolean โ†’ true, false
    • Undefined โ†’ let x; (No value assigned)
    • Null โ†’ let x = null; (Intentional absence of value)
    • Symbol โ†’ Symbol("id") (Unique identifier)
    • BigInt โ†’ 9007199254740991n (For very large integers)
  2. Reference Data Types (Mutable, stored by reference):
    • Object โ†’ { name: "Alice", age: 25 }
    • Array โ†’ [1, 2, 3]
    • Function โ†’ function greet() {}

2. [Google] What is the difference between primitive and reference types in JavaScript?

Answer:

  • Primitive Types: Stored by value (directly in memory).
  • Reference Types: Stored by reference (memory address stored, not the value itself).

Example:

let a = 10;
let b = a;  // Copy of value is assigned
b = 20;
console.log(a); // Output: 10

let obj1 = { name: "John" };
let obj2 = obj1; // Reference is assigned
obj2.name = "Doe";
console.log(obj1.name); // Output: "Doe" (both point to the same object)
  • Primitives are immutable, while reference types are mutable.

3. [Microsoft] What is typeof in JavaScript? Give examples.

Answer:
The typeof operator is used to determine the data type of a variable.

Examples:

console.log(typeof "Hello");    // Output: "string"
console.log(typeof 42);         // Output: "number"
console.log(typeof true);       // Output: "boolean"
console.log(typeof undefined);  // Output: "undefined"
console.log(typeof null);       // Output: "object"  // (Historical bug in JavaScript)
console.log(typeof {});         // Output: "object"
console.log(typeof []);         // Output: "object"
console.log(typeof function(){}); // Output: "function"

Note: typeof null returns "object" due to a bug in JavaScript since its early versions.


4. [Netflix] What is the difference between null and undefined?

Answer:

Featurenullundefined
MeaningIntentional absence of valueVariable is declared but not assigned
TypeObject (due to JS bug)Undefined
UsageUsed to manually clear a variableJavaScript assigns it automatically

Example:

let x; // undefined (not assigned)
let y = null; // null (assigned an empty value)
console.log(x); // Output: undefined
console.log(y); // Output: null

5. [PayPal] What is NaN in JavaScript? How do you check if a value is NaN?

Answer:

  • NaN (Not-a-Number) is a special numeric value that indicates an invalid mathematical operation.
  • Checking for NaN:
    • Using isNaN() (converts the value before checking): jsCopyEditconsole.log(isNaN("Hello")); // true console.log(isNaN(42)); // false
    • Using Number.isNaN() (checks without type conversion, more reliable): jsCopyEditconsole.log(Number.isNaN("Hello")); // false console.log(Number.isNaN(NaN)); // true

6. [Adobe] What is the difference between == and === when comparing data types?

Answer:

OperatorDescriptionExample
==Compares values only, performs type conversion"5" == 5 โ†’ true
===Compares both value and type, strict equality"5" === 5 โ†’ false

Example:

console.log(10 == "10");  // true (string converted to number)
console.log(10 === "10"); // false (types are different)

Best Practice: Always use === for strict comparisons.


7. [IBM] What are truthy and falsy values in JavaScript?

Answer:
In JavaScript, values can be classified as truthy or falsy when used in conditional statements.

Falsy values (evaluates to false in a boolean context):

  • false
  • 0, -0
  • "" (empty string)
  • null
  • undefined
  • NaN

Truthy values (everything else, including objects and non-empty strings):

  • "Hello"
  • 42
  • [] (empty array)
  • {} (empty object)
  • function() {}

Example:

if ("") { console.log("Truthy"); } else { console.log("Falsy"); } // Output: "Falsy"
if ("Hello") { console.log("Truthy"); } // Output: "Truthy"

8. [Tesla] Can JavaScript variables change data types dynamically?

Answer:
Yes, JavaScript is dynamically typed, meaning variables can change data types at runtime.

Example:

let x = 42;      // Number
x = "Hello"; // Now a String
x = true; // Now a Boolean
console.log(x); // Output: true

This flexibility allows quick prototyping but can also lead to unexpected bugs.


9. [Apple] What happens when you perform arithmetic operations on different data types?

Answer:
JavaScript automatically converts data types in arithmetic operations (Type Coercion).

Examples:

console.log("5" - 2);  // 3 (String converted to Number)
console.log("5" + 2);  // "52" (Number converted to String)
console.log("5" * "2"); // 10 (Both converted to Numbers)
console.log("5" - "hello"); // NaN

Best Practice: Use Number() or parseInt() to convert values explicitly and avoid surprises.


10. [Oracle] What is the difference between parseInt(), parseFloat(), and Number()?

Answer:

MethodConverts toExample
parseInt()Integer (removes decimal part)parseInt("42.9") โ†’ 42
parseFloat()Floating-point numberparseFloat("42.9") โ†’ 42.9
Number()Converts to exact numberNumber("42.9") โ†’ 42.9

Example:

console.log(parseInt("100px")); // 100
console.log(parseFloat("3.14 is pi")); // 3.14
console.log(Number("123abc")); // NaN (invalid number)