Friday, November 14, 2025
HomeLanguagesJavascriptLodash _.nth() Method

Lodash _.nth() Method

The _.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: This method accepts two parameters as mentioned above and described below:

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

Return Value: It returns the nth element of the array.

Example 1: It returns the third element of an array.




const _ = require('lodash');
  
let ar = [1, 2, 3, 4, 5]
  
let value = _.nth(ar, 3)
  
console.log(value)


Here, const _ = require('lodash') is used to import the lodash library into the file.

Output:

4

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




const _ = require('lodash');
  
let ar = [1, 2, 3, 4, 5]
  
let value = _.nth(ar, -3)
  
console.log(value)


Output:

3

Example 3: It returns undefined because there is no element at index 8.




const _ = require('lodash');
  
let ar = [1, 2, 3, 4, 5]
  
let value = _.nth(ar, 8)
  
console.log(value)


Output:

undefined

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32399 POSTS0 COMMENTS
Milvus
95 POSTS0 COMMENTS
Nango Kala
6765 POSTS0 COMMENTS
Nicole Veronica
11917 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11984 POSTS0 COMMENTS
Shaida Kate Naidoo
6889 POSTS0 COMMENTS
Ted Musemwa
7142 POSTS0 COMMENTS
Thapelo Manthata
6837 POSTS0 COMMENTS
Umr Jansen
6840 POSTS0 COMMENTS