VOOZH about

URL: https://www.geeksforgeeks.org/javascript/lodash-_-nth-method/

⇱ Lodash _.nth() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lodash _.nth() Method

Last Updated : 2 Sep, 2024

Lodash _.nth() method is used to return the nth index of the element. For a negative value of n, it returns the nth element from the end.

Syntax:

_.nth(array, n);

Parameters:

  • array: This parameter holds the query array.
  • n: This parameter holds the index of the element that is to be extracted.

Return Value:

  • It returns the nth element of the array.

Example 1: In this example, It returns the third element of an array.

Output:

4

Example 2: In this example, It returns the third element from the end of an array because the value of n is negative.

Output:

3

Example 3: In this example, It returns undefined because there is no element at index 8.

Output:

undefined

Reference: https://lodash.com/docs/4.17.15#nth

Comment