[X]

Off By One Join

luljs still works

Joining an array of four gives three separators.

In 2026: Still works, and it trips people building repeated strings. join places the separator between elements, so an array of length 4 produces 3 of them. Array(4).join("pizza") gives three, and Array(n) is a common way people accidentally get n minus 1 copies.

Where it came from: join inserts the separator between elements, so N elements yield N-1 separators, per MDN's join page. MDN

<script>
document.write('<p><code>Array(4).join("\ud83c\udf55")</code> is <b>' + Array(4).join("\ud83c\udf55") + '</b> (three, not four)</p>');
document.write('<p><code>["a", "b", "c"].join(" - ")</code> is <b>' + ["a","b","c"].join(" - ") + '</b></p>');
document.write('<p>join goes between elements, so N elements give N minus 1 separators.</p>');
</script>
sandboxed demo · breaks nothing but itselfrestart

More in luljs

all 63 in luljs › · the whole library ›