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.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.
length-1);
Error ts(2550) ― Property 'at' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2022' or later.
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.
length-1, 1);
const
const values:number[]
values = [1, 2, 3];
const values:number[]
values.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.
length-1, 99);
Error ts(2550) ― Property 'with' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.
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.at(-1);
Error ts(2550) ― Property 'at' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2022' or later.
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(-1, 1);
const
const values:number[]
values = [1, 2, 3];
const values:number[]
values.with(-1, 99);
Error ts(2550) ― Property 'with' does not exist on type 'number[]'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2023' or later.
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.