Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

lodash
communitycreator

What is the _.head() method in Lodash?

Shubham Singh Kshatriya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Overview

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

Syntax

_.head(array)
Syntax of the _.head() method

Parameter

This method accepts the following parameter:

  • array: This is the array to be queried.

Return value

This method returns the first element of the array.

Example

Let’s look at an example of the _.head() method in the code snippet below:

Console
Implementation of the _.head() method

Explanation

In the HTML tab:

  • Line 5: We import the lodash script.

In the JavaScript tab:

  • Line 2: We create an array numbers and populate it with a few values.
  • Line 5: We use the _.head() method to get the first element of the array.
  • Line 8: We print the output to the console.

Output

In the output, we see 5, which is the first element of the array.

RELATED TAGS

lodash
communitycreator

CONTRIBUTOR

Shubham Singh Kshatriya

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring