🔥JavaScript Interview Series(5): Data Types & Type Coercion

Welcome to our JavaScript Interview Series! Today, we’re tackling a fundamental yet tricky topic that catches many developers off guard: Data Types and Type Coercion. Understanding these concepts is crucial for writing robust and bug-free JavaScript co…


This content originally appeared on DEV Community and was authored by jackma

Welcome to our JavaScript Interview Series! Today, we're tackling a fundamental yet tricky topic that catches many developers off guard: Data Types and Type Coercion. Understanding these concepts is crucial for writing robust and bug-free JavaScript code. Let's dive into 10 common questions you might face.

1. What are the primitive data types in JavaScript?

  • Key Concept: This question tests your fundamental knowledge of JavaScript's basic building blocks.
  • Standard Answer: JavaScript has seven primitive data types: String, Number, Boolean, Null, Undefined, Symbol (introduced in ES6), and BigInt (introduced in ES2020). Primitives are immutable, meaning their values cannot be changed once created.
  • Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
    1. What is the difference between null and undefined?
    2. Can you explain what makes a value immutable?
    3. How does Symbol differ from other primitive types?

2. What is the difference between == and ===?

  • Key Concept: This assesses your understanding of equality operators and type coercion.
  • Standard Answer: The == (loose equality) operator compares two values for equality after performing type coercion, which means it converts the operands to the same type before comparison. The === (strict equality) operator compares both the value and the type of the operands without performing any type conversion. It's generally recommended to use === to avoid unexpected results from type coercion.
  • Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
    1. Can you provide an example where == returns true but === returns false?
    2. What is the result of null == undefined and null === undefined? Why?
    3. Are there any performance differences between the two operators?

3. Can you explain what "type coercion" is in JavaScript?

  • Key Concept: This probes your deeper knowledge of JavaScript's automatic type conversion mechanisms.
  • Standard Answer: Type coercion is the automatic or implicit conversion of values from one data type to another. This happens when you use operators on values of different types, such as adding a string to a number. For example, 1 + '2' results in the string '12' because the number 1 is coerced into a string.
  • Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
    1. What's the difference between implicit and explicit coercion? Can you give an example of each?
    2. How does the + operator behave differently from other arithmetic operators regarding coercion?
    3. What are some common pitfalls or bugs related to type coercion?

4. What will be the output of typeof null?

  • Key Concept: This is a classic "gotcha" question that tests your knowledge of a long-standing quirk in JavaScript.
  • Standard Answer: The output of typeof null is "object". This is a well-known bug in the original implementation of JavaScript that has been preserved for backward compatibility. While null is a primitive type, the typeof operator incorrectly identifies it as an object.
  • Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
    1. If typeof null is "object", how can you reliably check if a value is null?
    2. Can you explain why this bug exists from a historical perspective?
    3. What does typeof return for other primitive types?

5. What are "truthy" and "falsy" values in JavaScript?

  • Key Concept: This question evaluates your understanding of how JavaScript handles boolean contexts.
  • Standard Answer: In a boolean context, like an if statement, JavaScript coerces values into true or false. A falsy value is a value that is considered false when encountered in a boolean context. There are only eight falsy values in JavaScript: false, 0, -0, 0n (BigInt zero), "" (empty string), null, undefined, and NaN. All other values are considered truthy.
  • Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
    1. Is an empty array ([]) truthy or falsy? What about an empty object ({}) ?
    2. How would you write a function to check if a value is truthy?
    3. What is the output of new Boolean(false) in a conditional check?

6. Explain the difference between primitive types and reference types (objects).

  • Key Concept: This tests your understanding of how JavaScript stores and manages different kinds of data in memory.
  • Standard Answer: The main difference lies in how they are stored and passed. Primitive types are stored by value, meaning when you assign a primitive value to another variable, you are copying the actual value. Reference types (like objects, arrays, and functions) are stored by reference, meaning the variable holds a pointer to the location of the object in memory. When you assign an object to another variable, you are copying the reference, not the object itself.
  • Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
    1. If you change a property of an object through one variable, does it affect the other variable that references the same object? Why?
    2. How can you create a true copy (a deep copy) of an object?
    3. How does this concept apply to passing arguments to functions?

7. What is NaN and how can you check for it?

  • Key Concept: This question focuses on a specific, often misunderstood, value within the Number type.
  • Standard Answer: NaN stands for "Not-a-Number". It's a property of the global object and a special value of the Number type, typically returned when a mathematical operation fails (e.g., parseInt('hello') or Math.sqrt(-1)). A tricky characteristic of NaN is that it does not equal itself (NaN === NaN is false). Therefore, you cannot check for it using equality operators. The correct way to check for NaN is with the Number.isNaN() function.
  • Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
    1. What is the difference between the global isNaN() function and Number.isNaN()?
    2. What is the typeof NaN?
    3. Can you name two operations that would result in NaN?

8. What does the !! (double negation) operator do?

  • Key Concept: This tests your knowledge of a common shortcut for explicit type coercion to boolean.
  • Standard Answer: The double negation operator (!!) is a concise way to explicitly coerce any value to its corresponding boolean value. The first ! operator coerces the value to the opposite of its truthiness (e.g., !'hello' becomes false). The second ! negates it back, resulting in the actual boolean representation of the value (e.g., !!'hello' becomes true).
  • Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
    1. Is using !! more performant than using the Boolean() constructor?
    2. Can you provide a practical use case where !! would be useful?
    3. What would be the result of !!null or !!0?

9. Explain the output of "5" - 3 versus "5" + 3.

  • Key Concept: This is a classic coercion question that highlights the unique behavior of the + operator.
  • Standard Answer:
    • For "5" - 3, the output is 2. The subtraction operator (-) works only on numbers, so it triggers numeric coercion, converting the string "5" into the number 5 before performing the subtraction.
    • For "5" + 3, the output is "53". The addition operator (+) is overloaded; it performs both numeric addition and string concatenation. If either operand is a string, it will convert the other operand to a string and concatenate them.
  • Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
    1. What would be the output of "5" * 3 and "5" / 3?
    2. How would you ensure that "5" + 3 results in the number 8?
    3. What happens if you use the + operator with an object or an array?

10. How does JavaScript coerce an object to a primitive value?

  • Key Concept: This advanced question explores the internal mechanisms JavaScript uses for object-to-primitive conversion.
  • Standard Answer: When an object needs to be converted to a primitive, JavaScript follows a specific process. It first checks for the existence of the [Symbol.toPrimitive] method. If it exists, it's called. If not, it falls back to two methods: valueOf() and toString(). For numeric contexts (like subtraction), it tries valueOf() first, then toString(). For string contexts (like concatenation), it tries toString() first, then valueOf().
  • Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
    1. Can you create a custom object and override its toString() method to demonstrate this behavior?
    2. What is the purpose of the [Symbol.toPrimitive] method?
    3. How does the Date object behave when coerced?


This content originally appeared on DEV Community and was authored by jackma


Print Share Comment Cite Upload Translate Updates
APA

jackma | Sciencx (2025-10-24T17:42:57+00:00) 🔥JavaScript Interview Series(5): Data Types & Type Coercion. Retrieved from https://www.scien.cx/2025/10/24/%f0%9f%94%a5javascript-interview-series5-data-types-type-coercion/

MLA
" » 🔥JavaScript Interview Series(5): Data Types & Type Coercion." jackma | Sciencx - Friday October 24, 2025, https://www.scien.cx/2025/10/24/%f0%9f%94%a5javascript-interview-series5-data-types-type-coercion/
HARVARD
jackma | Sciencx Friday October 24, 2025 » 🔥JavaScript Interview Series(5): Data Types & Type Coercion., viewed ,<https://www.scien.cx/2025/10/24/%f0%9f%94%a5javascript-interview-series5-data-types-type-coercion/>
VANCOUVER
jackma | Sciencx - » 🔥JavaScript Interview Series(5): Data Types & Type Coercion. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/24/%f0%9f%94%a5javascript-interview-series5-data-types-type-coercion/
CHICAGO
" » 🔥JavaScript Interview Series(5): Data Types & Type Coercion." jackma | Sciencx - Accessed . https://www.scien.cx/2025/10/24/%f0%9f%94%a5javascript-interview-series5-data-types-type-coercion/
IEEE
" » 🔥JavaScript Interview Series(5): Data Types & Type Coercion." jackma | Sciencx [Online]. Available: https://www.scien.cx/2025/10/24/%f0%9f%94%a5javascript-interview-series5-data-types-type-coercion/. [Accessed: ]
rf:citation
» 🔥JavaScript Interview Series(5): Data Types & Type Coercion | jackma | Sciencx | https://www.scien.cx/2025/10/24/%f0%9f%94%a5javascript-interview-series5-data-types-type-coercion/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.