Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 77 ยท JavaScript Interview Questions

JavaScript: Types, Coercion and Equality

Prepare for JavaScript interview questions on primitives, typeof quirks, == versus ===, truthy and falsy values, NaN and Object.is, with runnable examples.

Types and equality are the most common JavaScript warm-up questions, because they reveal whether you have actually been bitten by the language. Interviewers ask "what does [] + {} give?" not because you will write that, but because explaining it shows you understand conversion rules. This lesson collects the questions that come up again and again, with the reason behind each answer.

If you want the full explanation of the rules, see Data Types and Type Conversion. Here the focus is answering well: short definition, tiny example, pitfall.

Primitives versus objects

Seven primitives, one object type

number, bigint Doubles and arbitrary integers.
string, symbol Text and unique keys.
boolean true or false.
undefined, null Two kinds of "nothing".

Everything else, arrays and functions included, is an object.

1 / 2

Q: What are the primitive types? string, number, bigint, boolean, undefined, null and symbol. Primitives are immutable and compared by value. Objects, including arrays, functions, dates and maps, are compared by reference.

JS Value versus reference

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Follow-up: "Why can you call 'abc'.toUpperCase() on a primitive?" JavaScript temporarily wraps the primitive in a wrapper object (String) to look up the method, then discards the wrapper. The string itself is still a primitive.

typeof and its quirks

typeof returns a string naming the type. It is right for most things and famously wrong for two.

JS typeof quirks

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Q: Why does typeof null return "object"? It is a bug from the first version of the language, kept because fixing it would break existing code. Check for null with value === null.

Q: Why does typeof on an undeclared variable not throw? typeof is the one operator that tolerates an undeclared name and returns "undefined". Reading the bare name would throw a ReferenceError.

== versus ===

=== (strict equality) never converts: different types are never equal. == (loose equality) converts operands following fixed rules before comparing. The accurate short answer: use === by default; == is predictable but easy to misread.

The rules worth remembering:

  • null == undefined is true, and neither equals anything else with ==.
  • If one side is a number and the other a string, the string becomes a number.
  • A boolean becomes a number first (true is 1, false is 0).
  • An object compared with a primitive is converted to a primitive first (through valueOf or toString).
JS Loose equality puzzles

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The last line is the classic argument against ==: it is not transitive. The one common, defensible use is value == null to test "null or undefined" in one expression.

Truthy and falsy

Q: What are the falsy values? There are exactly eight: false, 0, -0, 0n, "", null, undefined and NaN. Everything else is truthy, including "0", "false", [], {} and new Boolean(false).

JS Truthy and falsy checks

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Pitfall: || returns the first truthy operand, so 0 and "" are replaced. The nullish operator ?? only replaces null and undefined.

NaN and Object.is

NaN is the result of an invalid numeric operation (0 / 0, Number("abc")). It is the only value not equal to itself.

JS NaN, isNaN and Object.is

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Q: How is Object.is different from ===? Only in two cases: Object.is(NaN, NaN) is true and Object.is(0, -0) is false. Otherwise they agree.

Famous addition puzzles

+ is overloaded: if either operand is a string after conversion to primitive, it concatenates; otherwise it adds numbers. Other arithmetic operators always convert to numbers.

JS Why these results?

Press Run (or Ctrl + Enter) to see the console output here.

No output.

If you are asked about 0.1 + 0.2, name the cause: numbers are IEEE 754 binary doubles, and 0.1 has no exact binary representation. Money is best kept in integer cents.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does typeof null return?

    It is a long-standing historical bug. Use value === null to test for null.
  2. 2. Which expression is true?

    NaN is not strictly equal to itself, but Object.is treats it as the same value. Object.is(0, -0) is false, and two separate arrays are different references.
  3. 3. Which value is truthy?

    Any non-empty string is truthy, including "0". The other three are falsy.
  4. 4. What does 1 + 2 + "3" evaluate to?

    Evaluation goes left to right: 1 + 2 is 3, then 3 + "3" concatenates to "33".
  5. 5. Why is Number.isNaN safer than the global isNaN?

    The global function converts the argument to a number first, so isNaN("hello") is true. Number.isNaN only returns true for the actual NaN value.

Summary

  • Seven primitives; everything else is an object. Primitives compare by value, objects by reference.
  • typeof null is "object" and arrays report "object"; use === null and Array.isArray.
  • Prefer ===; know the loose-equality rules, especially null == undefined.
  • Eight falsy values; use ?? instead of || when 0 or "" are valid.
  • NaN !== NaN; Object.is differs from === only for NaN and -0.
  • + concatenates when a string is involved; other operators convert to numbers.

Next: JavaScript: Scope, Hoisting and Closures