OneBite.Dev - Coding blog in a bite size

get the nth element of array in Javascript

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

  function getNthElement(array, n) {
    return array[n-1];
  }

This code is used to retrieve an element from an array in Javascript. The function takes two parameters, the array and the nth element index (starting from 1). The index is then decremented by 1 as in Javascript, arrays are 0-indexed. Finally, the function will return the element at the specified index in the array.

javascript