In this article, we will learn to get the first & last element in an array in Javascript, along with understanding their implementation through the examples.
Approach 1: Using length property in JavaScript
A javascript array is a variable that holds multiple values at a time. The first and last elements are accessed using an index the first value is accessed using index 0 and the last element can be accessed through the length property which has one more value than the highest array index. The array length property in JavaScript is used to set or return the number of elements in an array.
Example 1: This example illustrates to access the first and last number in the array.
Javascript
let s = [3, 2, 3, 4, 5];
function Gfg() {
let f = s[0];
let l = s[s.length - 1];
console.log("First element is " + f);
console.log("Last element is " + l);
}
Gfg();
|
Output
First element is 3
Last element is 5
Example 2: This example illustrates to access the first and last word in the array.
Javascript
let s = ["Geeks", "for", "geeks", "computer", "science"];
function Gfg() {
let f = s[0];
let l = s[s.length - 1];
console.log("First element is " + f);
console.log("Last element is " + l);
}
Gfg();
|
Output
First element is Geeks
Last element is science
The Array.pop() method removes the last element of the array and returns it and the Array.shift() method removes the first element from the array and returns it.
Example: In this example, we will be using the array pop() and shift() methods to get the last and first element of the array.
Javascript
let s = [3, 2, 3, 4, 5];
function Gfg() {
let f = s.shift(0);
let l = s.pop();
console.log("first element is " + f);
console.log(" Last element is " + l);
}
Gfg();
|
Output
first element is 3
Last element is 5
Array.slice() method returns the part of the array by slicing it with provided index and length.
Example: In this example, we will be using the slice() method to get the first and the last element of the array.
Javascript
let s = [3, 2, 3, 4, 5];
function Gfg() {
let f = s.slice(0, 1);
let l = s.slice(-1);
console.log("first element is " + f);
console.log("Last element is " + l);
}
Gfg();
|
Output
first element is 3
Last element is 5
Array.filter() method returns a filtered array that removes the element which returns false for the callback function.
Example: In this example, we will be using the filter() method to get the first and the last element of the array.
Javascript
let s = [3, 2, 3, 4, 5];
function Gfg() {
let [f, l] = s.filter((item, i) =>
(i == 0) || (i == s.length - 1));
console.log("first element is " + f);
console.log(" Last element is " + l);
}
Gfg();
|
Output
first element is 3
Last element is 5
The Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in the variable array where there is more than 1 value is expected. It allows us the privilege to obtain a list of parameters from an array.
Example:
Javascript
const array = [1, 2, 3, 4, 5];
const [firstItem, ...rest] = array;
const lastItem = rest.pop();
console.log('First item:', firstItem);
console.log('Last item:', lastItem);
|
Output
First item: 1
Last item: 5
Approach 6: Using Array.at() Method
The Array.at() method in JavaScript allows to access elements of an array by their index. To get the first and last elements of an array using the at() method, pass the desired indices to the method. The first element has an index of 0. Array last element can be accessed by using the array’s length-1 as the index.
Example:
Javascript
function gfg() {
let s = ["Geeks", "for", "geeks", "computer", "science"];
console.log(s.at(0))
console.log(s.at(s.length - 1))
}
gfg();
|
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!