How to useArray.filter() Method in Javascript

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
// Array
let s = [3, 2, 3, 4, 5];
function Gfg() {

    // Storing the first item and second in a variable
    let [f, l] = s.filter((item, i) => 
                (i == 0) || (i == s.length - 1));

    // Printing output to screen
    console.log("first element is " + f);
    console.log(" Last element is " + l);
}
Gfg(); // Calling the function

Output
first element is 3
 Last element is 5

Get the first and last item in an array using JavaScript

JavaScript array is used to store multiple elements in a single variable. It is often used when we want to store a list of elements and access them by a single variable. We will learn how to get the first & last element in an array in JavaScript, along with understanding their implementation through the examples.

These are the following approaches:

Table of Content

  • Approach 1: Using length property in JavaScript
  • Approach 2: Using Array.pop() and Array.shift() Method
  • Approach 3: Using Array.slice() Method
  • Approach 4: Using Array.filter() Method
  • Approach 5: Using Spread Operator
  • Approach 6: Using Array.at() Method
  • Approach 7: Using Array.find() Method

Similar Reads

Approach 1: Using length property in JavaScript

The array length property in JavaScript is used to set or return the number of elements in an array....

Approach 2: Using Array.pop() and Array.shift() Method

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....

Approach 3: Using Array.slice() Method

Array.slice() method returns the part of the array by slicing it with provided index and length....

Approach 4: Using Array.filter() Method

Array.filter() method returns a filtered array that removes the element which returns false for the callback function....

Approach 5: Using Spread Operator

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....

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....

Approach 7: Using Array.find() Method

The Array.find() method returns the value of the first element in the array that satisfies the provided testing function. However, in this context, we can cleverly use find() with specific conditions to obtain the first and last elements of the array....