find the first element in an array in javascript

To find the first element in an array in JavaScript, you can use indexing. The first element in an array has an index of 0, so you can simply access it using square brackets notation.

const arr = [1, 2, 3, 4, 5];
const firstElement = arr[0];

console.log(firstElement); // Output: 1
99 chars
5 lines

In this example, we declare an array arr containing the values [1, 2, 3, 4, 5]. We then access the first element in the array using arr[0] and assign it to a constant variable firstElement. Finally, we log the value of firstElement to the console, which would be 1.

related categories

gistlibby LogSnag