OneBite.Dev - Coding blog in a bite size

get the first element of array in Javascript

Code snippet on how to get the first element of array in Javascript

  let array = [1, 2, 3, 4, 5];
  let firstElement = array[0];

This code snippet uses the Javascript array and index access syntax to get the first element of an array. The first line declares an array named ‘array’ which contains five elements - the numbers 1 to 5. The second line declares a variable called ‘firstElement’ and assigns to it the value of the array at index 0, which is the first element of the array. This is also known as array indexing, as we access the element at a specific index of the array. In this case, the index we are using is 0, which corresponds to the first element of the array.

javascript