How to get last element of an array in typescript

In this typescript tutorial, we will see how to get last element of an array in typescript using different methods like length property, slice function, at function, and pop function in typescript. Here we are going to cover the below topics:

  • Typescript last element of the array
  • Typescript last element of array type
  • Typescript removes the last element of an array
  • Typescript gets the first and the last element of an array
  • Typescript gets the last n elements of the array
  • Typescript gets the last 10 elements of an array

Typescript last element of the array

Here we will see how to get the last element of an array using a typescript.

Method 1: Length method

For example, we will use the length properties to get the last element of an array in typescript.

Length method in typescript, is used to get the number of elements available in an array.

In the code editor, create a file named as ‘lastElementArray.ts’ file write the below code:

const names = [ 'Ron', 'San', 'Rock' ];

// Outputs: 'Rock'
console.log(names[names.length - 1]);

To compile the code and run the below command and you can see the result in the console.

ts-node lastElementArray
Typescript last element of the array
Typescript last element of the array

Method 2: Using at method

For example, we will use the at method to get the last item from an array in typescript.

The at() method in typescript accepts an integer number and returns the element at that index, which can be positive or negative.

Syntax

at(index)

This function is only applicable when the typescript config’s lib property is set to es2022. To set this config to es2022, write the below command in the console.

 tsc --init

Then tsconfig.json file will open, change the target property to es2022.

Typescript gets the last items of an array
Typescript gets the last items of an array

In the code editor, create a file named as ‘lastElementArray.ts’ file write the below code.

 const arr = [ 'Tim', 'Bob', 'Mike' ];

  // Outputs: 'Mike'
  console.log(arr.at(-1));

To compile the code and run the below command and you can see the result in the console.

ts-node lastElementArray
Typescript gets the last items of an array using at method
Typescript gets the last items of an array using at method

This is an example on how to get the last element of the array in typescript.

Read How to check if string is empty in typescript

Typescript last element of the array type

Here we will see how to get the last element of an array type in typescript.

For example, to get the last element from an array we will use the slice method.

The slice method in typescript extracts part of an array and returns a new array.

Syntax of slice method:

 array.slice( start [,end] ); 

This function accepts two parameters, as stated above and explained below:

start: The zero-based index at which to begin extraction.
end: The zero-based index at which to finish extraction.

In the code editor, create a file named ‘lastElementArray.ts’ file and write the below code.

const names = [ 'Ron', 'San', 'Rock' ];

// Outputs: 'Rock'
console.log(names.slice(- 1)[0] );

Compile the code and run the below command and you can see the result in the console.

ts-node lastElementArray.ts
Typescript last element of the array type
Typescript last element of the array type

This is an example of getting the last element of the array type in typescript.

Typescript removes the last element of an array

Here we will see an example that removes the last element of an array in typescript.

For example, we will use the pop method to remove the last element from an array.

The Pop() in typescript, is used to remove the last item of an array and returns that element.

Syntax of pop() in typescript:

array.pop()

In the code editor, create a file named ‘lastElementArray.ts’ file and write the below code.

const names = [ 'Ron', 'San', 'Rock' ];

// Outputs: 'Rock'
console.log(names.pop());
console.log(names)

Compile the code and run the below command and you can see the result in the console.

ts-node lastElementArray
Typescript removes the last element of an array
Typescript removes the last element of an array

This is an example to remove the last element of an array in typescript.

Read How to check type of variable in Typescript

Typescript gets the first and the last element of an array

Here we will see how to get the first and last item of an array in typescript.

For example, we have an array, from this we will extract the first and last element from an array.

In the code editor, create a file named ‘lastElementArray.ts’ file and write the below code.


function getElements() {
     
    // Storing the first item in a variable
    let firstItem=names[0];
     
    // Storing the last item in a variable
    let lastItem=names[names.length-1];
     
   // Printing output to screen
   console.log("First item is "+ firstItem);
   console.log("Last item is "+ lastItem);
  }
  getElements(); // Calling the function

Compile the code and run the below command and you can see the result in the console.

ts-node lastElementArray
Typescript gets the first and the last element of an array
Typescript gets the first and the last element of an array

This is an example to gets the first and the last element of an array in typescript.

Typescript gets the last n elements of the array

Here we will see how to get the last n elements of an array in typescript.

For example, we will use the slice method to get the last n element of an array in typescript.

The Array.slice() function returns a new array having the original array’s last N items.

In the code editor, create a file named ‘lastElementArray.ts’ file and write the below code.

const names = [ 'Ron', 'San', 'Rock' ];
function getElements() {
     const lastNItem= names.slice(-2);
     console.log("Last 2 items are "+ lastNItem);
}
getElements();

To compile this code, run the below command and you can see the result in the console.

ts-node lastElementArray
Typescript gets the last n elements of the array
Typescript gets the last n elements of the array

This is an example of how to get the last n elements of the array in typescript.

Read How to add a property to object in Typescript

Typescript gets the last 10 elements of an array

Here we will see an example to get the last 10 elements of an array in typescript.

For example, we will get the last 10 elements of an array using slice method in typescript.

In the code editor, create a file named ‘lastElementArray.ts’ file and write the below code.

const names = [ 'Ron', 'San', 'Rock','Alex','Ruby','Jack','Mike','John','Hely','Lily','Bob','Henry' ];
function getElements() {
     const lastNItem= names.slice(Math.max(names.length - 10, 1));
     console.log("Last 10 items are "+ lastNItem);
}
 getElements();

To compile this code, run the below command and you can see the result in the console.

ts-node lastElementArray
Typescript gets the last 10 elements of an array
Typescript gets the last 10 elements of an array

This is an example to get the last 10 elements of an array in typescript.

Conclusion

In this Typescript tutorial, we saw different examples to get the last element from an array using different typescript methods. Here are the examples we covered:

  • Typescript last element of the array
  • Typescript last element of array type
  • Typescript removes the last element of an array
  • Typescript gets the first and the last element of an array
  • Typescript gets the last n elements of the array
  • Typescript gets the last 10 elements of an array

You may like the following typescript tutorials:

>