Friday, October 24, 2025
HomeLanguagesJavascriptLodash _.reverse() Function

Lodash _.reverse() Function

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.reverse() is used to reverse the array. This function changes the original array and also returns a new array.

Syntax: 

_.reverse( array )

Parameters: This function accepts single parameter array that holds the array of elements.

Return Value: It returns the reversed array.

Note: Please install lodash module by using npm install lodash before using the below given code.

Example 1: This examples shows that this function effect the original array.

javascript




// Requiring the lodash library
const _ = require("lodash");
  
// Original array
let array1 = [1, 2, 3, 4]
  
// Printing original Array
console.log("original Array1: ", array1)
  
// Using _.reverse() method
let reversedArray = _.reverse(array1);
  
// Printing the reversedArray
console.log("reversed Array: ", reversedArray)
  
// Printing original Array
console.log("original Array1: ", array1)


Output: 

Example 2: In this example, we will take an array of objects and reverse them.

javascript




// Requiring the lodash library
const _ = require("lodash");
  
// Original array
let array1 = [
    { "a": 1 }, 
    { "a": 2, "b": 3 }, 
    { "c": 3 }
]
  
// Printing original Array
console.log("original Array1: ", array1)
  
// Using _.reverse() method
let reversedArray = _.reverse(array1);
  
// Printing the reversedArray
console.log("reversed Array: ", reversedArray)
  
// Printing original Array
console.log("original Array1: ", array1)


Output: 

RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS