How to use the reverse method in Lodash

Overview

The reverse method reverses an array. The changes will be made in the original array.

Syntax

_.reverse(array)

Arguments

This method takes the array to be reversed as an argument.

Return value

This method reverses the original array provided as an argument and returns it.

Code example

The below code demonstrates the use of the reverse method in Lodash:

Console
Using the reverse method

Code explanation

  • Line 6: We load the Lodash library file from the server link.

  • Line 8: We create an array arr with [1,2,3,4,5] as elements.

  • Line 11: We use the reverse method to reverse the array. This will reverse the order of the elements on the source array and returns it. Now the array is [5,4,3,2,1].

Free Resources