Last Element

Completed Code

var arr=[ 'We', 'love', 'learning', 'JavaScript!','It', 'is', 'a', 'fantastic', 'language' ];

last = function(array) {
    return array[array.length - 1];
};

console.log(last(arr));

Explanation

The code for this challenge is very similar to the previous one, however there is one small change. Rather than using an index, we use the length property of the array. Length is NOT zero-based, in order to get the last element we have to subtract one to call the proper index. If we didn't subtract one, our output would be undefined. This array does not have an element with an index of six.

Last updated