I found out that if we do a code to get first and last arrays like this:
function firstLast(array) {
var first = array[0];
var last = array[array.length - 1];
return [first, last];
}
It works, I found that solution to get last array in the internet, but I can’t figure out why the - 1 would give the last instead of the first index since the array itself is counting 0, 1, 2, 3... from the lowest to highest. So why is that? The ++ would not work for that? Can someone give me explanation and/or examples please?
array.length - 1is?-1, it'sarray.length - 1instead; which should indeed give you the last indexarray.lengthwill return 5, so you need to substract 1 to get 4.