var x = "something" is evaluated, and
x is assigned to a String type. The same process applies to lists - you don't need to define a list's type, you can just fill the list up with values.
A list is defined by putting the elements in between the square brackets (
), where the elements can be heterogeneous (don't have to be of the same type):
var myList = ["Peter", "Smith", 25, 23, "Random String", 10]
Get Last Element Using length
length attribute of a list denotes the number of elements in a list:
, referencing the array. The last element has the index of
var lastElement = myList[myList.length - 1]; console.log(lastElement);
This results in:
Get Last Element Using pop()
pop() method returns the last element of a collection, but removes it during the process. Though, if we use the spread operator from ES6 - we can avoid this functionality by temporarily copying the list in memory, and performing
pop() on the copy, keeping the original list intact.
The pop() Method
pop() the last element off the list:
var lastElement = myList.pop(); console.log(lastElement) console.log(myList)
This results in the list being cut short of that element:
10 ["Peter", "Smith", 25, 23, "Random String"]
If you don't need the original list to stay intact, then you can pop elements off of it, but if you do need the original, you can make a copy and
pop() from it.
pop() with ES6' Spread Operator
The feature that we can leverage here is the spread operator (
Amongst other features, the spread operator can copy lists/arrays:
This generates a new list, with the same elements as
myList. That being said - we can
pop() from the new list instead of the original one:
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
This results in:
10 ["Peter", "Smith", 25, 23, "Random String", 10]
Note: This may be convenient to write, but is not the cheapest of solutions, since it copies a list. For small lists, it doesn't reduce the performance by much, but with longer lists, this is an inefficient way to simply get the last element.
Get Last Element Using slice()
slice(a, b) method 'slices' the array from point
a to point
b-1 - given that
b are integers. In a more mathematical way, you can look at slice like this:
[a, b) - the element at
a is included, whilst the element at
b is not:
This results in a slice of the original array:
["Smith", 25, 23]
slice() method also accepts negative indexing - where it iterates through the collection backwards. So if we
slice(-4, -1), the iteration will start from the third element from the back and end at one before the last one (because
slice doesn't include the
This allows us to simply include one last element, from the end of the list via
Which results in:
This is still an array, so to get this element we access it with
myList.slice(-1) - which will return our element:
slice() is best used when you want to retrieve the last N elements of an array, rather than one, since you can simply use
Get Last Element Using reverse()
reverse() method reverses the list in-place. The last element will become the first, and vice-versa:
The output will be:
[10, 'Random String', 23, 25, 'Smith', 'Peter']
Now our element is the first one, so we can access it with
Note: Reversing an array/list is also a potentially very expensive operation. Avoid reversing the collection if the only thing you want to do is access the last element. If you are to reverse it anyway - you can access the element like this. If not, refer to the first approach of using
The cheapest, most efficient, and probably most intuitive method of these is the first -
myList[myList.length()-1]. This is what you'll be using most of the time.
pop() removes the element from the list and returns it. If you want to remove the element - this is the approach you'll want to be using, since otherwise, you'll have to remove the element manually.
slice() gives us a slice, which is a great approach if we want to get the last N elements.
reverse() reverses the array in-place and should be avoided if you just want to get an element. If you're reversing the array anyway, though, this doesn't cost anything extra.