The _.first() method is used to get the first element of the specified array.
Syntax:
_.first(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
const _ = require( 'lodash' );
console.log(_.first([5]));
console.log(_.first([2, 4, 6]));
console.log(_.first([ "gfg" , "GFG" ]));
|
Output:
5
2
gfg
Example 2:
Javascript
const _ = require( 'lodash' );
console.log(_.first([]));
|
Output:
undefined
Note: This will not work in normal JavaScript because it requires the Lodash library to be installed.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks 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!
Last Updated :
01 Mar, 2021
Like Article
Save Article