← Back to JavaScript, page 7

javascript-0131 · Easy fresher

Predict the output of these type coercion and operator puzzles, and name the rule behind each result.

The plus operator concatenates when either side is a string and otherwise adds, while minus, multiply, and divide always convert to numbers. Comparison of two strings is alphabetical, a mixed comparison converts to numbers, and objects convert through valueOf and toString. Most surprises follow from these three rules plus the conversion of empty arrays, null, and undefined.

Full answer

Work through each puzzle with the rule, not by memory.

Rule 1: + with a string on either side concatenates, after converting the other side to a string. Otherwise it adds numbers. Arrays and objects first become primitives: [] becomes '', and {} becomes '[object Object]'. So [] + [] is '', [] + {} is '[object Object]', and 1 + '2' is '12'. Evaluation is left to right, so 1 + 2 + '3' is '33', while '1' + 2 + 3 is '123'.

Rule 2: -, *, / and % always convert both sides to numbers. So '5' - 2 is 3, '5' * '2' is 10, and 'abc' - 1 is NaN. The unary plus converts to a number too: +'42' is 42, +'' is 0, +[] is 0, +{} is NaN, and +null is 0, but +undefined is NaN.

Rule 3: loose equality == and relational operators convert. If both sides are strings, they are compared as text, so '10' < '9' is true. If one side is a number, the other becomes a number, so '10' < 9 is false. null == undefined is true, but null == 0 is false, because == treats null and undefined specially. null >= 0 is true, because relational operators convert null to 0. NaN is not equal to anything.

More traps: [1, 2] + [3] is '1,23' because both arrays become strings. [] == ![] is true: ![] is false, then [] becomes '', and both sides become 0. typeof null is 'object', a historic bug. 0.1 + 0.2 === 0.3 is false because of floating point. 'b' + 'a' + +'a' + 'a' gives 'baNaNa' because +'a' is NaN.

Advice: in real code, use ===, convert explicitly with Number() and String(), and use template literals for text. Puzzles are useful for learning the rules, but they should not appear in production code.

javascript
const show = (label, value) => console.log(label.padEnd(22), typeof value === 'string' ? JSON.stringify(value) : value);

show('1 + 2 + "3"', 1 + 2 + '3');
show('"1" + 2 + 3', '1' + 2 + 3);
show('[] + []', [] + []);
show('[] + {}', [] + {});
show('[1, 2] + [3]', [1, 2] + [3]);
show('"5" - 2', '5' - 2);
show('"5" * "2"', '5' * '2');
show('"abc" - 1', 'abc' - 1);
show('+"" +[] +null', [+'', +[], +null]);
show('+undefined +{}', [+undefined, +{}]);
show('"10" < "9"', '10' < '9');
show('"10" < 9', '10' < 9);
show('null == undefined', null == undefined);
show('null == 0', null == 0);
show('null >= 0', null >= 0);
show('[] == ![]', [] == ![]);
show('"b" + "a" + +"a" + "a"', 'b' + 'a' + +'a' + 'a');
show('typeof null', typeof null);
show('true + true', true + true);
show('"3" + 4 * "2"', '3' + 4 * '2');
show('[10, 9, 1].sort()', [10, 9, 1].sort());
show('[] + null + 1', [] + null + 1);
show('!!"false" !!"" !!0n', [!!'false', !!'', !!0n]);
show('1 < 2 < 3, 3 > 2 > 1', [1 < 2 < 3, 3 > 2 > 1]);
show('NaN == NaN', NaN == NaN);

const money = { valueOf: () => 42, toString: () => 'forty two' };
show('money + 1', money + 1);
show('`${money}`', `${money}`);
show('money * 2', money * 2);

Run with Node.js v24.22.1 as js-131.js; two runs gave identical output. Output: 1 + 2 + "3" "33" "1" + 2 + 3 "123" [] + [] "" [] + {} "[object Object]" [1, 2] + [3] "1,23" "5" - 2 3 "5" * "2" 10 "abc" - 1 NaN +"" +[] +null [ 0, 0, 0 ] +undefined +{} [ NaN, NaN ] "10" < "9" true "10" < 9 false null == undefined true null == 0 false null >= 0 true [] == ![] true "b" + "a" + +"a" + "a" "baNaNa" typeof null "object" true + true 2 "3" + 4 * "2" "38" [10, 9, 1].sort() [ 1, 10, 9 ] [] + null + 1 "null1" !!"false" !!"" !!0n [ true, false, false ] 1 < 2 < 3, 3 > 2 > 1 [ true, false ] NaN == NaN false money + 1 43 `${money}` "forty two" money * 2 84

Key points

  • Plus concatenates if either side is a string after primitive conversion; minus, multiply, and divide convert to numbers.
  • Evaluation is left to right, so the position of the first string matters.
  • Unary plus converts to a number: empty string, empty array, and null give 0.
  • Two strings compare as text; mixed comparisons convert to numbers; null and undefined are only loosely equal to each other.
  • Use strict equality and explicit conversion in real code.

Common mistakes

  • Using == and expecting type-safe results.
  • Adding user input from forms without converting it, which concatenates strings.
  • Sorting numbers with the default sort, which compares text.
  • Memorizing puzzle answers without the three conversion rules.

Follow-ups

  • What is the difference between valueOf and toString when an object is converted?
  • How does Symbol.toPrimitive change conversion?
  • Why does typeof null return object?

Tips

  • Explain each answer through a rule, and name the rule.
  • Remember left to right evaluation for chained plus operators.
  • Close by saying that you use === and explicit conversion in real code.