Get The First And Last Element Of An Array In JavaScript

To get the first element of an array in JavaScript, use the arr[0] syntax. To get the last element of an array, use the arr.length-1 syntax.

get the first and last element of an array in javascript

Get The First And Last Element Of An Array In JavaScript

Using Array Index

In JavaScript, arrays are zero-based: the first element of an array is at index 0, and the last element is at the index equal to the value of the array’s length property minus 1.

So, to get the first and last element of an array, you would need to access elements at indices 0 and length – 1. For example, given an array like this:

var arr = [1, 2, 3, 4];

The first element would be arr[0], which would return 1. The last element would be arr[arr.length – 1], which would return 4.

Here is the complete code –

var arr = [1, 2, 3, 4];

var first = arr[0];

var last = arr[arr.length-1];

console.log(first); //1

console.log(last); //4

If you call arr[0] and arr[arr.length-1] on an empty array, you will get undefined.

Here is an example:

var arr = [];

console.log(arr.length); //0

console.log(arr); //[ ]

console.log(arr[0]); //undefined

console.log(arr[arr.length-1]); //undefined

If you want to avoid getting undefined, you can check if the array is empty first:

var arr = [];

if (arr.length > 0) {

  console.log(arr[0]);

  console.log(arr[arr.length-1]);

} else {

  console.log("Array is empty");

}

Output: Array is empty

As you can see, getting the first and last element of an array is quite simple. Just remember to account for the fact that arrays are zero-based (the first element is at index 0) when accessing elements.

Using shift() And pop() Methods

You could also use the shift() and pop() methods to get the first and last element of an array, respectively. However, these methods modify the original array, which is not always what you want.

To get the first element of an array in JavaScript, use the arr.shift() method. This method removes the first element from an array and returns it.

For example, given an array like this:

var arr = [‘a’, ‘b’, ‘c’, ‘d’];

The first element would be arr.shift(), which would return ‘a’. The remaining elements in the array would be [‘b’, ‘c’, ‘d’].

To get the last element of an array in JavaScript, use the arr.pop() method. This method removes the last element from an array and returns it.

For example, given an array like this:

var arr = [‘a’, ‘b’, ‘c’, ‘d’];

The last element would be arr.pop(), which would return ‘d’. The remaining elements in the array would be [‘a’, ‘b’, ‘c’].

Here is the complete code:

var arr = [‘a’, ‘b’, ‘c’, ‘d’];

var first = arr.shift();

var last = arr.pop();

console.log(first); //’a’

console.log(last); //’d’

As you can see, using the shift() and pop() methods is a convenient way to get the first and last element of an array without having to access elements by their indices. However, these methods do modify the original array, which is not always what you want.

To avoid modifying the original array, you could create a copy of the array, and then use the shift() and pop() methods on the copy.

For example:

var arr = [‘a’, ‘b’, ‘c’, ‘d’];

var arrCopy = arr.slice();

var first = arrCopy.shift();

var last = arrCopy.pop();

console.log(first); //"a"

console.log(last); //"d"

console.log(arr); //["a", "b", "c", "d"]

As you can see, the original array is not modified when you use the shift() and pop() methods on a copy of the array.

Conclusion

In this article, you learned how to get the first and last element of an array in JavaScript.

You also learned about the shift() and pop() methods, which can be used to get the first and last element of an array, respectively. Just remember that these methods do modify the original array.

Leave a Reply