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.
99 chars5 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
.
gistlibby LogSnag