Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.filter(':last') vs. .last()

I was wondering if there is any difference between .filter(':last') and .last()?

For me it looks like they're doing the same, but I'm new to jQuery. If there is no difference in the result, which one is recommended or is it just a matter of personal preference?

like image 265
eLwoodianer Avatar asked Feb 17 '23 06:02

eLwoodianer


1 Answers

last works by saying "give me the last element from the selection". It takes just two function calls and four lines of code to do so. It can't be done in a quicker way.

filter(':last'), however, is much more complex. It is a much more flexible system, allowing multiple elements to be returned if that's what you want, or multiple conditions, or a mixture of both. It is much less efficient, because it has to work out what you want. For instance, parsing ':last' takes a little time, whereas with the last function it's a simple property lookup.

last is by far the more efficient.

like image 177
lonesomeday Avatar answered Feb 19 '23 04:02

lonesomeday