There's a new,
very simple array method in ES2022,
which is the At Method.
And so let's take a look at it.
And let's start by creating some dummy array.
So, 23, 11, 64.
So just three random values here.
And then, if we wanted to take one of the values
out of the array,
let's say the first one,
then we would traditionally do this, right?
So array at position zero.
So that's basically what this means.
But now with the new At Method,
we can do the exact same thing using a method.
And, as you can guess, that is the At Method.
Then we specify the exact same index here,
and then we get the exact same value.
So basically now we do what we say here.
So here we say, array at position zero.
And so that's why this new method is called
array dot at position zero.
So very, very simple, right?
So basically we can now replace
the traditional bracket notation
with the more modern looking At Method,
if we prefer to use array methods like this.
Now, maybe this doesn't look all too useful.
So, what's the big deal here?
Well, actually there is one particularity of the At Method,
which makes it quite useful to use
instead of the brackets notation.
So, let's now say that we wanted to get
the last element of the array.
Now, supposing that we do not know the length of the array,
we would write something like this.
So, array at the position of
array dot length minus one.
And that will then give us 64.
So, array dot length is the length of the array,
which a three,
but the last position here is position number two
because the array is zero-based.
So the indexes are zero-based.
So zero, one, two.
And so therefore,
whenever we want to get the last element of an array,
we always subtract one from the length.
So this is quite a common scenario in JavaScript.
And that's why there's also another solution.
So another way is to use the slice method
that we just learned before.
So arr dot slice.
And with this,
we can then get a copy of the original array.
And here we can use minus one to get
the last element of the array, right?
So just like we learned in the previous lecture.
So, here we get that copy of the array
only with the last element.
So of course we want the value,
and so then we need to take out that first value
by doing square brackets zero.
Now, right.
So these are the two more traditional ways of solving
the problem of getting the last element.
However, as you can probably guess,
the new At Method makes this process even easier.
So now all we have to do is to write arr dot at,
and now here we can write the exact same negative in nexus,
then we can write in the slice method.
And so if we lock this now,
then here again we have 64.
So the negative index, just like in slice,
basically starts counting from the right side here.
So from the end of the array.
And so we could also write, minus two.
So, minus one and minus two.
So this should give us 11.
And indeed here is that 11.
But most of the time,
the thing that we're most interested in here
is this minus one, to get the last element of the array.
And that's actually it.
Now the only question is,
should you use this new At Method
or should you keep using the bracket notation?
Well, as always, it depends.
So, if you want to get to the last element of an array,
or basically start counting from the end of an array,
then you should probably start using the At Method.
Also, if you want to do something called "method chaining",
which we will talk about later in this section,
then the At Method is also perfect for that.
So basically combining multiple methods
all at the same time.
And then, it's quite helpful to use
the At Method instead of the brackets notation.
Now, on the other hand,
if you just want to quickly get a value from an array,
so just like the first element,
then of course you can keep using the brackets notation.
And personally, I also do that all the time.
So basically if all you want to do is something like this,
then you can simply keep using the square brackets.
Okay.
Oh, and by the way,
I actually also wanted to let you know that the At Method
also works on strings.
So, let's look to the console.
A string,
let's say jonas and then here we can also use
the At Method.
For example, let's say, at zero.
And so that will give us J,
and then of course we can also
get the last character off the string
by using at minus one.