'Why [,,].join(' ') length is one less than array length?
What is the reason behind the length of string produced by joining var arr = [,,], like so: var str = arr.join(' ') being one less than length of arr.
var literal_arr = [,,],
joined_literal_arr = literal_arr.join(' '),
constructor_arr = new Array(2),
joined_constructor_arr = new Array(2).join(' ');
console.log("Literal array notation length = ", literal_arr.length);
console.log("Joined literal array notation string length = ", joined_literal_arr.length);
console.log("Constructor array notation length = ", constructor_arr.length);
console.log("Joined constructor notation string length = ", joined_constructor_arr.length);
Solution 1:[1]
It is simple as .join will join array elements with separator provided as argument. It won’t append separator after the last element or prepend it before the first element. It will place separator between elements only.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sebastian Simon |
