Your product could be here, in front of thousands of JavaScript developers. Sponsor this spot →
JavaScript Quiz

JavaScript Type Coercion Quiz

JavaScript converts values from one type to another whenever an operator gets types it doesn’t expect. The + operator switches to string concatenation as soon as one side is a string. Loose equality (==) converts both sides, usually to numbers, before it compares them. Comparison operators turn booleans into 1 and 0.

These rules explain most of JavaScript’s famous “wat” moments. Learn them and you’ll be able to predict the output of code that looks impossible at first glance, which is exactly what interviewers like to test.

Type Coercion questions

  1. Why does ('b' + 'a' + + 'a' + 'a') return "banana"?
  2. Why does 1 + "2" + "2" return "122"?
  3. Why is false == '0' true in JavaScript?
  4. Why is 3 > 2 > 1 false in JavaScript?
  5. What do true == [] and true == ![] return?
  6. Why does [] + {} return "[object Object]"?
  7. What is the difference between == and === in JavaScript?
  8. Why does "5" * "2" equal 10 in JavaScript?
  9. Is an empty array truthy in JavaScript?
  10. What happens when you use an object as an object key?
  11. Why is [1, 2, 3] == "1,2,3" true?
  12. What is the difference between parseInt and Number?

More topics