Its kinda weird that the JavaScript Array class does not offer a last method to retrieve the last element of an array. I know the solution is simple (Ar[Ar.length-1] ), but, still, this is too frequently used.

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise.
If this question can be reworded to fit the rules in the help center, please edit the question.

39

For cases where you don't mind altering the array as a side-effect (ie. where the array is only temporary anyway), the idiom would be item= array.pop();.
–
bobinceJul 13 '10 at 8:08

Good heavens, after looking at that perf page, it appears array[array.length-1] is way faster than the others.
–
JondlmMay 30 '13 at 14:59

@JondIm but if you create an array in function, you need to invent local name for it (which leads to names such as arr2), and you have 2 lines of code instead of oneliner
–
Danubian SailorJun 7 '13 at 13:04

@Sean - I thought I'd stay out of the business of recommending a particular Javascript library to the original poster. Google does a pretty good job of assessing the web's collective opinion on which library to use. Why would I suggest a particular one? Indeed, why have you recommended underscore.js, which seems very flavor-of-the-month to me at first glance?
–
TriptychOct 3 '11 at 23:18

6

I actually prefer the answer below of implementing the function outside of a library. That said, as someone that stumbled upon this question via Google, I was suggesting that the top answer help others continue their search for a solution rather than sending them to the back button.
–
Sean LynchOct 4 '11 at 18:38

5

The question was "Why isn't this feature built into Javascript" not "How can achieve this functionality". There is no reason to think the original author was looking for how to actually write his own last() function. The question was about the nature of the development of the Javascript core language itself.
–
TriptychOct 4 '11 at 18:53

3

But if @Nikhil wants to know how to implement it - underscore has a wonderful annotated source. This implementation of last() is quite robust, probably more then is needed by this author but great for a library. documentcloud.github.com/underscore/docs/…
–
reconbotJun 1 '12 at 14:35

@UpTheCreek, because you don't need to store the array to a variable.
–
rmobisNov 29 '12 at 19:11

3

@ÁlvaroG.Vicario This is fine for an array of references, but in your example you're working with numbers. As per the MDN doc: "slice copies strings and numbers into the new array. Changes to the string or number in one array does not affect the other array." If the developer wants to get a reference "array.last" in order to do something with the value of the last element in their original array, and the array values happen to be string or number literals, this method will not work.
–
1nfinitiMay 21 '13 at 15:10

note that adding properties to Array's prototype can break code where for..in is used to iterate over an array. Using for..in to iterate an array is bad practice, but it's done commonly enough that altering Array's prototype is also bad practice. In general, prototypes of Object, Array, String, Number, Boolean, and Date should not be altered if your script needs to work with other unknown code.
–
Dagg NabbitJul 13 '10 at 8:35

7

@no - Thanks for the tip. I should've mentioned that the reason for adding the ES5 syntax with enumerability set to false was precisely to solve the for..in problem. Sure, we're not there yet with a wide implementation of ES5, but it's good enough to know now as browsers are catching up to it fast, including IE9.
–
AnuragJul 13 '10 at 8:49

4

For empty lists, this.length - 1 evaluates to -1, which, because it is a negative number, is treated as an array property, not an element index.
–
claymationAug 6 '12 at 20:23

null I think- because you do have an array well defined- just that there are no valid objects in it. undefined should be used only when last 'property' is not defined on the called container.
–
Nikhil GargJul 13 '10 at 7:52

3

just returning this[l-1] would give you undefined as is normal for accessing non-existent properties. Personally I'd rather JS threw an exception rather than returning undefined, but JS prefers to sweep errors under the rug.
–
bobinceJul 13 '10 at 8:04

Came here looking for an answer to this question myself. The slice answer is probably best, but I went ahead and created a "last" function just to practice extending prototypes, so I thought I would go ahead and share it. It has the added benefit over some other ones of letting you optionally count backwards through the array, and pull out, say, the second to last or third to last item. If you don't specify a count it just defaults to 1 and pulls out the last item.