Navigate back to the homepage

How to get last element of array in typescript?

Infinitbility
TypeScript
May 9th, 2022 · 1 min read
keep affilitate

Hi Friends 👋,

Welcome to Infinitbility ❤️!

Today, we are going to learn How to get the last element of the array in typescript?, here we will use basic array length syntax to find the last element of the array.

So, when we want to know how many elements are in an array we will use array.length and it will return a number of elements in an array.

Well, what we can do here, is we will use array.length minus 1 so we will get the last index of an array.

When we pass the index in an array it will return the last element of an array.

Let’s see what we are going to do…

  1. create sample array list variable
  2. get the last index of the array
  3. get the last element from the array using the index
1// create sample array list variable
2const domains = ["infinitbility.com", "aguidehub.com", "sortoutcode.com"];
3
4// get last index of array
5let lastIndex = domains.length;
6
7// get last element from array
8domains[lastIndex - 1];

When you run the above code, you will get sortoutcode.com in your console and which is the last element of the array.

TypeScript, get last element of array example
TypeScript, get last element of array example

All the best 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

melio affilitate

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to get random number in typescript?

TypeScript, get random number example with step by step guide.

May 8th, 2022 · 1 min read

How to get value from object by key in typescript?

TypeScript, get value from object by key example with step by step guide.

May 7th, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]