JavaScript - Get first and last Item of JS Array Last Updated : 25 Nov, 2024 Comments Improve Suggest changes Like Article Like Report These are the following ways to get first and last element of the given array: 1. Using length PropertyThe array length property in JavaScript is used to set or return the number of elements in an array. JavaScript let arr = [3, 2, 3, 4, 5]; let f = arr[0]; let l = arr[arr.length - 1]; console.log(f, l) Output3 5 2. Using Array.pop() and Array.shift() MethodThe 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. JavaScript let arr = [3, 2, 3, 4, 5]; let f = arr.shift(0); let l = arr.pop(); console.log(f, l) Output3 5 3. Using Array.slice() MethodThe Array.slice() method returns the part of the array by slicing it with provided index and length. JavaScript let arr = [3, 2, 3, 4, 5]; let f = arr.slice(0, 1); let l = arr.slice(-1); console.log(f, l) Output[ 3 ] [ 5 ] 4. Using Array.filter() MethodThe Array.filter() method returns a filtered array that removes the element which returns false for the callback function. JavaScript let arr = [3, 2, 3, 4, 5]; let [f, l] = arr.filter((item, i) => (i == 0) || (i == arr.length - 1)); console.log(f, l) Output3 5 5. Using Spread OperatorThe 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. JavaScript const arr = [1, 2, 3, 4, 5]; const [f, ...rest] = arr; const l = rest.pop(); console.log(f, l) Output1 5 6. Using Array.at() MethodThe 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. JavaScript const arr = [1, 2, 3, 4, 5]; let f = arr.at(0); let l = arr.at(arr.length - 1); console.log(f, l) Output1 5 Comment More infoAdvertise with us Next Article JavaScript - Get first and last Item of JS Array bestharadhakrishna Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Questions +1 More Similar Reads How to Add Elements to a JavaScript Array? Here are different ways to add elements to an array in JavaScript.1. Using push() MethodThe push() method adds one or more elements to the end of an array and returns the new length of the array.Syntaxarray.push( element1, element2, . . ., elementN );JavaScriptconst arr = [10, 20, 30, 40]; arr.push( 3 min read Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J 3 min read Insert at the Beginning of an Array in JavaScript Following are different ways to add new elements at the beginning of an array1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element 2 min read Remove Elements From a JavaScript Array Here are the various methods to remove elements from a JavaScript ArrayRemove elements from Array1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed.javascriptlet a = ["Apple", 6 min read Remove Duplicate Elements from JavaScript Array To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r 3 min read How to Remove Multiple Elements from Array in JavaScript? Here are various methods to remove multiple elements from an array in JavaScript1. Using filter() MethodThe filter() method creates a new array with the elements that pass the condition. This method does not change the original array.JavaScriptlet a = [1, 2, 3, 4, 5]; let remove = [2, 4]; a = a.filt 3 min read Insert at the Beginning of an Array in JavaScript Following are different ways to add new elements at the beginning of an array1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element 2 min read Reverse an Array in JavaScript Here are the different methods to reverse an array in JavaScript1. Using the reverse() MethodJavaScript provides a built-in array method called reverse() that reverses the elements of the array in place. This method mutates the original array and returns the reversed array.JavaScriptlet a = [1, 2, 3 3 min read JavaScript - Delete last Occurrence from JS Array These are the following ways to remove the last Item from the given array: 1. Using pop() Method (Simple and Easiest Method for Any Array) The pop() method is used to get the last element of the given array it can also be used to remove the last element from the given array. JavaScriptlet a = [34, 2 2 min read Remove Empty Elements from an Array in JavaScript Here are different approaches to remove empty elements from an Array in JavaScript.1. Using array.filter() MethodThe array filter() method is used to create a new array from a given array consisting of elements that satisfy given conditions.array.filter( callback( element, index, arr ), thisValue )J 3 min read Like