Lodash _.first() Method

The _.first() method is used to get the first element of the specified array.

Syntax:

_.head(array)

Parameters: This method accepts a single parameter as mentioned above and described below:

  • array: This parameter holds an array to query.

Return Value: This method returns the first element of the array.

Example 1:



Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining Lodash variable
const _ = require('lodash');
  
// Calling _.first() functions
console.log(_.first([5]));
console.log(_.first([2, 4, 6]));
console.log(_.first(["gfg", "GFG"]));

chevron_right


Output:

5
2
gfg

Example 2:

Javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining Lodash variable
const _ = require('lodash');
  
// Calling _.first() functions
console.log(_.first([]));

chevron_right


Output:

undefined

Note: This will not work in normal JavaScript because it requires the lodash library to be installed.

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.