#355·wtfjs

"Adding arrays" explanation code in `README.md` throws runtime `TypeError` due to ASI collision

Author: codeCraft-RitikCreated Sep 14, 2026Updated Sep 14, 2026

Issue : "Adding arrays" explanation code in README.md throws runtime TypeError due to ASI collision

Severity & Impact

  • Severity: High (Handbook Correctness & Reliability)
  • Impact: In a book specifically dedicated to explaining the quirks of JavaScript and Automatic Semicolon Insertion (ASI), the step-by-step explanation code under "Adding arrays" contains an unintentional ASI omission. Executing or testing the explanation snippet throws an unhandled TypeError.

Affected File

Root Cause Analysis

In README.md under ## Adding arrays:

javascript
[1, 2, 3] +
  [4, 5, 6][
    // call toString()
    (1, 2, 3)
  ].toString() +
  [4, 5, 6].toString();
// concatenation
"1,2,3" + "4,5,6";
// ->
("1,2,34,5,6");

Because no semicolon separated the initial demonstration [1, 2, 3] + [4, 5, 6] from the subsequent explanation step [1, 2, 3].toString(), the JavaScript parser (and subsequent Prettier runs) interpreted [4, 5, 6] followed by [(1, 2, 3)] as a computed property lookup:

  1. The comma expression (1, 2, 3) evaluates to 3.
  2. [4, 5, 6][3] evaluates to undefined (out-of-bounds index).
  3. undefined.toString() is invoked, immediately throwing TypeError: Cannot read properties of undefined (reading 'toString').

Minimal Reproducible Example

Run the snippet from README.md in Node:

javascript
[1, 2, 3] +
  [4, 5, 6][
    (1, 2, 3)
  ].toString() +
  [4, 5, 6].toString();

Actual Output

TypeError: Cannot read properties of undefined (reading 'toString')

Expected Output

A valid step-by-step breakdown illustrating how operands are coerced to strings.

Proposed Fix

Restructure the explanation so each evaluation phase is syntactically distinct:

diff
--- a/README.md
+++ b/README.md
@@ -574,8 +574,8 @@ What if you try to add two arrays?
 The concatenation happens. Step-by-step, it looks like this:
 
 ```js
-[1, 2, 3] +
-  [4, 5, 6][
-    // call toString()
-    (1, 2, 3)
-  ].toString() +
-  [4, 5, 6].toString();
+// 1. Both arrays are coerced to primitives using toString()
+[1, 2, 3].toString() + [4, 5, 6].toString();
+
 // concatenation
 "1,2,3" + "4,5,6";