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), andBigInt(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)
- What is the difference between
nullandundefined? - Can you explain what makes a value immutable?
- How does
Symboldiffer from other primitive types?
- What is the difference between
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)
- Can you provide an example where
==returnstruebut===returnsfalse? - What is the result of
null == undefinedandnull === undefined? Why? - Are there any performance differences between the two operators?
- Can you provide an example where
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 number1is coerced into a string. - Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
- What's the difference between implicit and explicit coercion? Can you give an example of each?
- How does the
+operator behave differently from other arithmetic operators regarding coercion? - 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 nullis"object". This is a well-known bug in the original implementation of JavaScript that has been preserved for backward compatibility. Whilenullis a primitive type, thetypeofoperator incorrectly identifies it as an object. - Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
- If
typeof nullis"object", how can you reliably check if a value isnull? - Can you explain why this bug exists from a historical perspective?
- What does
typeofreturn for other primitive types?
- If
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
ifstatement, JavaScript coerces values intotrueorfalse. A falsy value is a value that is consideredfalsewhen encountered in a boolean context. There are only eight falsy values in JavaScript:false,0,-0,0n(BigInt zero),""(empty string),null,undefined, andNaN. All other values are considered truthy. - Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
- Is an empty array (
[]) truthy or falsy? What about an empty object ({}) ? - How would you write a function to check if a value is truthy?
- What is the output of
new Boolean(false)in a conditional check?
- Is an empty array (
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)
- If you change a property of an object through one variable, does it affect the other variable that references the same object? Why?
- How can you create a true copy (a deep copy) of an object?
- 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
Numbertype. - Standard Answer:
NaNstands for "Not-a-Number". It's a property of the global object and a special value of theNumbertype, typically returned when a mathematical operation fails (e.g.,parseInt('hello')orMath.sqrt(-1)). A tricky characteristic ofNaNis that it does not equal itself (NaN === NaNisfalse). Therefore, you cannot check for it using equality operators. The correct way to check forNaNis with theNumber.isNaN()function. - Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
- What is the difference between the global
isNaN()function andNumber.isNaN()? - What is the
typeof NaN? - Can you name two operations that would result in
NaN?
- What is the difference between the global
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'becomesfalse). The second!negates it back, resulting in the actual boolean representation of the value (e.g.,!!'hello'becomestrue). - Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
- Is using
!!more performant than using theBoolean()constructor? - Can you provide a practical use case where
!!would be useful? - What would be the result of
!!nullor!!0?
- Is using
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 is2. The subtraction operator (-) works only on numbers, so it triggers numeric coercion, converting the string"5"into the number5before 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.
- For
- Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
- What would be the output of
"5" * 3and"5" / 3? - How would you ensure that
"5" + 3results in the number8? - What happens if you use the
+operator with an object or an array?
- What would be the output of
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()andtoString(). For numeric contexts (like subtraction), it triesvalueOf()first, thentoString(). For string contexts (like concatenation), it triestoString()first, thenvalueOf(). - Possible Follow-up Questions: 👉 (Want to test your skills? Try a Mock Interviews)
- Can you create a custom object and override its
toString()method to demonstrate this behavior? - What is the purpose of the
[Symbol.toPrimitive]method? - How does the
Dateobject behave when coerced?
- Can you create a custom object and override its
This content originally appeared on DEV Community and was authored by jackma
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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.