Array methods like .at(), .slice(), and .splice() accept negative indices to access elements from the end of an array or string.
Using array.slice(array.length - 2) is less readable than array.slice(-2).
Returns a copy of a section of an array.
For both start and end, a negative index can be used to indicate an offset from the end of the array.
For example, -2 refers to the second to last element of the array.
@param ― start The beginning index of the specified portion of the array.
If start is undefined, then the slice begins at index 0.
@param ― end The end index of the specified portion of the array. This is exclusive of the element at the index 'end'.
If end is undefined, then the slice extends to the end of the array.
slice(
const values:number[]
values.
Array<number>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
Returns a copy of a section of an array.
For both start and end, a negative index can be used to indicate an offset from the end of the array.
For example, -2 refers to the second to last element of the array.
@param ― start The beginning index of the specified portion of the array.
If start is undefined, then the slice begins at index 0.
@param ― end The end index of the specified portion of the array. This is exclusive of the element at the index 'end'.
If end is undefined, then the slice extends to the end of the array.
slice(
const values:number[]
values.
Array<number>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length-2,
const values:number[]
values.
Array<number>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
length-1);
const
const values:number[]
values = [1, 2, 3];
const values:number[]
values.
Array<number>.at(index: number): number |undefined
Returns the item located at the specified index.
@param ― index The zero-based index of the desired code unit. A negative index will count back from the last item.
at(
const values:number[]
values.
Array<number>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
@param ― start The zero-based location in the array from which to start removing elements.
@param ― deleteCount The number of elements to remove. Omitting this argument will remove all elements from the start
paramater location to end of the array. If value of this argument is either a negative number, zero, undefined, or a type
that cannot be converted to an integer, the function will evaluate the argument as zero and not remove any elements.
@returns ― An array containing the elements that were deleted.
splice(
const values:number[]
values.
Array<number>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
Copies an array, then overwrites the value at the provided index with the
given value. If the index is negative, then it replaces from the end
of the array.
@param ― index The index of the value to overwrite. If the index is
negative, then it replaces from the end of the array.
@param ― value The value to write into the copied array.
@returns ― The copied array with the updated value.
with(
const values:number[]
values.
Array<number>.length: number
Gets or sets the length of the array. This is a number one higher than the highest index in the array.
Returns a copy of a section of an array.
For both start and end, a negative index can be used to indicate an offset from the end of the array.
For example, -2 refers to the second to last element of the array.
@param ― start The beginning index of the specified portion of the array.
If start is undefined, then the slice begins at index 0.
@param ― end The end index of the specified portion of the array. This is exclusive of the element at the index 'end'.
If end is undefined, then the slice extends to the end of the array.
Returns a copy of a section of an array.
For both start and end, a negative index can be used to indicate an offset from the end of the array.
For example, -2 refers to the second to last element of the array.
@param ― start The beginning index of the specified portion of the array.
If start is undefined, then the slice begins at index 0.
@param ― end The end index of the specified portion of the array. This is exclusive of the element at the index 'end'.
If end is undefined, then the slice extends to the end of the array.
Returns a copy of a section of an array.
For both start and end, a negative index can be used to indicate an offset from the end of the array.
For example, -2 refers to the second to last element of the array.
@param ― start The beginning index of the specified portion of the array.
If start is undefined, then the slice begins at index 0.
@param ― end The end index of the specified portion of the array. This is exclusive of the element at the index 'end'.
If end is undefined, then the slice extends to the end of the array.
slice(-2, -1);
const
const values:number[]
values = [1, 2, 3];
const values:number[]
values.
Array<number>.at(index: number): number |undefined
Returns the item located at the specified index.
@param ― index The zero-based index of the desired code unit. A negative index will count back from the last item.
Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
@param ― start The zero-based location in the array from which to start removing elements.
@param ― deleteCount The number of elements to remove. Omitting this argument will remove all elements from the start
paramater location to end of the array. If value of this argument is either a negative number, zero, undefined, or a type
that cannot be converted to an integer, the function will evaluate the argument as zero and not remove any elements.
@returns ― An array containing the elements that were deleted.
Copies an array, then overwrites the value at the provided index with the
given value. If the index is negative, then it replaces from the end
of the array.
@param ― index The index of the value to overwrite. If the index is
negative, then it replaces from the end of the array.
@param ― value The value to write into the copied array.
@returns ― The copied array with the updated value.
Returns a copy of a section of an array.
For both start and end, a negative index can be used to indicate an offset from the end of the array.
For example, -2 refers to the second to last element of the array.
@param ― start The beginning index of the specified portion of the array.
If start is undefined, then the slice begins at index 0.
@param ― end The end index of the specified portion of the array. This is exclusive of the element at the index 'end'.
If end is undefined, then the slice extends to the end of the array.
Returns a copy of a section of an array.
For both start and end, a negative index can be used to indicate an offset from the end of the array.
For example, -2 refers to the second to last element of the array.
@param ― start The beginning index of the specified portion of the array.
If start is undefined, then the slice begins at index 0.
@param ― end The end index of the specified portion of the array. This is exclusive of the element at the index 'end'.
If end is undefined, then the slice extends to the end of the array.
If you prefer explicit .length - index calculations for documentation purposes, or if you have a codebase convention that requires them for readability in certain contexts, you may want to disable this rule.