

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Get the first and last item in an array using JavaScript?
Javascript arrays are 0-indexed. This means that the first element is at the 0th position. The last element is at the length-of-array - 1th position. So we can access these elements using −
Example
arr[0] // First element arr[arr.length - 1] // last element For example, let arr = [1, 'test', {}, 'hello'] console.log(arr[0]) console.log(arr[arr.length - 1])
Output
1 hello
- Related Questions & Answers
- Java Program to Get First and Last Elements from an Array List
- Get the last item from node list without using length property in JavaScript?
- Get the item that appears the most times in an array JavaScript
- First element and last element in a JavaScript array?
- Get the first element in an array and return using MongoDB Aggregate?
- Split First name and Last name using JavaScript?
- Get greatest repetitive item in array JavaScript
- Getting only the first item for an array property in MongoDB?
- Program to get the last element from an array using C#
- How to get first and last date of the current month with JavaScript?
- Get the first element of array in JavaScript
- Find first duplicate item in array in linear time JavaScript
- How to get the last element in JavaScript array?
- How to get the first n values of an array in JavaScript?
- How to get only the first n% of an array in JavaScript?
Advertisements