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

Overview

The _.initial() method in Lodash is used to get all the elements of an array except the last element.

Syntax

_.initial(array)
_.initial() syntax

Parameters

This method accepts the following parameter:

  • array: The array to be queried.

Return value

This method returns all the elements of the array except the last element.

Example

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

Console
Implementation of the _.initial() 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 _.initial() method to get all the array elements except the last element.
  • Line 8: We print the output to the console.

Output

In the output, we see all the elements of the array except the last element.

.

Free Resources