Preface: Note that the Array[3] in your console output is just how the console is showing it to you. Your newArr is really:
["Martin", 78, 67, ['L', 'M', 'P']]
It happens because that's how slice is defined. Loosely speaking it:
- Creates a new, empty array
- Reads the
length property of what you give it
- Loops through from the starting index (default
0) to the ending index (default length - 1); call the loop variable k
- Puts any property found in the object when asking it for property
k into the array at index k - start.
- Sets the
length of the returned array to k - start.
- Returns the array
Or (again very loosely):
function slice(start, end) {
var n, k, result;
start = arguments.length < 1 ? 0 : +start;
end = arguments.length < 2 ? +this.length : +end;
result = [];
for (var k = start, n = 0; k < end; ++k, ++n) {
if (n in this) {
result[n] = this[k];
}
}
result.length = n;
return result;
}
All the gory details.
Since your arrLike object has a length property and has properties with the names 0, 1, 2, and 3, you get the result you've shown.