VOOZH about

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

⇱ Lodash _.head() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lodash _.head() Method

Last Updated : 2 Sep, 2024

Lodash _.head() method is used to get the first element of an array.

Syntax

_.head( array );

Parameters:

  • array: This parameter holds the query array.

Return Value:

  • It returns the first element of the array.

Example 1: In this example, we are getting the first element of the given array by the use of the _.head() method.

Output:

1

Example 2: In this example, we are getting the first element of the given array of objects by the use of the _.head() method.

Output:

{ a: 1, b: 2 }

Example 3: In this example, we are getting undefined as the given arrray is empty.

Output:

undefined 

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

Comment