An Interest In:
Web News this Week
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
- March 27, 2024
January 20, 2022 01:42 pm GMT
Original Link: https://dev.to/codewithsnowbit/best-way-to-get-last-item-in-an-array-5a30
Best Way to Get Last Item in an Array
Hello Folks
What's up, this is SnowBit here. I am a young passionate and self-taught developer and have an intention to become a successful developer.
Today, I am here with a basic topic mostly for beginners and revisers.
Avoid this
const arr = ["JS", "PY", "CSS"]const lastItem = arr[2]
Don't do this for getting the last item of an array. Let me explain, this is a small array and contains only 3 items that are easily countable but, what if there are hundred's of items in an array then this method will make you mad
Do this
const arr = ["JS", "PY", "CSS"]const lastItem = arr[arr.length - 1]
Here, arr.length
becomes dynamic, it increases as you increase items in arr
no matter how many you add and that will not make you mad
Thank you for reading, have a nice day!
Your appreciation is my motivation
- Follow me on Twitter - @codewithsnowbit
- Subscribe me on YouTube - Code With SnowBit
Original Link: https://dev.to/codewithsnowbit/best-way-to-get-last-item-in-an-array-5a30
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To