1const array = [1,2,3,4,5];
2const firstElement = array.shift();
3
4// array -> [2,3,4,5]
5// firstElement -> 1
1var items = ["Item 1", "Item 2", "Item 3"];
2var firstItem = typeof items[0] !== "undefined" ? items[0] : null;
3
4console.log(firstItem);
1let array = [1,2,3] // makes your array
2array[0] // returns first element of your array.
1// Method - 1 ([] operator)
2var arr = [1, 2, 3, 4, 5];
3var first = arr[0];
4console.log(first);
5/*
6 Output: 1
7*/
8
9// Method - 2 (Array.prototype.shift())
10var arr = [1, 2, 3, 4, 5];
11var first = arr.slice(0, 1).shift();
12console.log(first);
13/*
14 Output: 1
15*/
16
17// Method - 3 (Destructuring Assignment)
18var arr = [1, 2, 3, 4, 5];
19const [first] = arr;
20console.log(first);
21/*
22 Output: 1
23*/
1let array = ["a", "b", "c", "d", "e"];
2
3// Both first1 and first2 have the same value.
4let [first1] = array;
5let first2 = array[0];