Get The First Two Elements Of An Array In JavaScript

To get the first two elements of an array in JavaScript, you can use any of these methods –

  1. Use the Array.slice() method to get a shallow copy of the array and pass the start index as 0, and end index as 2. For Example – `arr.slice(0, 2)`
  2. Use the destructuring assignment syntax to get the first two elements from an array. For Example – `const arr = [“a”, “b”, “c”]; const [first, second, …rest] = arr`

Let’s discuss these methods in detail below.

get the first two elements of an array in javascript

Array.slice() To Get The First Two Elements Of An Array In JavaScript

The Array.slice() method is used to create a shallow copy of the array and returns a new array. This method accepts two arguments –

  1. The start index from where you want to extract elements from the original array. In our case, it will be 0 (the first element of the array).
  2. The end index till which you want to extract the elements from the original array. In our case, it will be 2 (2 will not be included).

Note that the slice() method does not mutate or change the original array. If you want to get only the first two elements of an array in JavaScript and don’t want to use any third-party library, then you can use the slice() method as follows –

const arr = ["a", "b", "c"];

const firstTwoElements = arr.slice(0, 2);

console.log(firstTwoElements); // outputs ['a', 'b']

In the above code, we have defined an array called arr. We have created a new array called firstTwoElements by using the slice() method on the arr array.

Destructuring Assignment Syntax To Get The First Two Elements Of An Array

Another way to get only the first two elements from an array in JavaScript is by using the destructuring assignment syntax. This method is available in ES6 (ECMAScript 2015).

In this method, we use the const keyword to create a variable and then use the square brackets [] to assign values from an array to individual variables. Let’s understand this with a code example.

const arr = ["a", "b", "c"];

const [first, second, …rest] = arr;

console.log(first); // outputs 'a'

console.log(second); // outputs 'b'

In the above code, we have defined an array called arr. We have used the destructuring assignment syntax to get the first and second elements from the arr array into two variables – first and second.

Note that the …rest variable is used to get all remaining elements of the array starting from index 2.

In the destructuring assignment syntax method, the order in which you assign the variables is important. The first variable will get the value of the first element of the array, the second variable will get the value of the second element of the array and so on.

Conclusion

In this article, you have learned how to get the first two elements of an array in JavaScript. You can use any of these methods –

Use the Array.slice() method to get a shallow copy of the array and pass the start index as 0, and end index as 2.

Use the destructuring assignment syntax to get the first two elements from an array.

I hope this article was helpful to you. Please feel free to share your feedback and suggestions in the comment section below. Thank you for reading!

Leave a Reply