Last updated on Sep 24, 2020 by Suraj Sharma
const array = ['India', 'United States', 'Brazil']; const firstElement = array; // 'India' const lastElement = array[array.length-1]; // 'Brazil'
We can use the
0 as a first parameter and
1 as a second parameter.
It returns a new array of length 1.
const array = ['India', 'United States', 'Brazil']; const subArray = array.slice(0, 1); // ['India']
The Array destructuring assignment introduced in ES6 can help us get the first element of an array.
const array = ['India', 'United States', 'Brazil']; const [firstElement] = array; console.log(firstElement) // 'India';