The Wayback Machine - https://web.archive.org/web/20240920014719/https://www.geeksforgeeks.org/lodash-introduction/
Open In App

Lodash Introduction

Last Updated : 10 Dec, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. It provides us with various inbuilt functions and uses a functional programming approach that makes coding in JavaScript easier to understand because instead of writing repetitive functions, tasks can be accomplished with a single line of code. It also makes it easier to work with objects in javascript if they require a lot of manipulation to be done upon them.

Installation: It can be used directly using the CDN link or can be installed using npm or yarn.

Method 1: We can directly use the Lodash file in the browser. Go to the official documentation and copy the lodash.min.js file CDN link and paste this link inside the head section.

<script type = “text/JavaScript” src = “https://cdn.jsdelivr.net/npm/lodash@4.17.20/lodash.min.js”></script>

Example:

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
    <script type="text/JavaScript" src=
    </script>
</head>
  
<body>
    <script>
        console.log(_.isNumber(100));
        console.log(_.isNumber('GeeksforGeeks courses'));
        console.log(_.isNumber(78.43));
    </script>
</body>
  
</html>


Output:

true
false
true

Method 2: We can install it by using npm. Make sure that you have installed Node.js and npm.

npm install lodash

If you are using yarn then you can use the following command:

yarn install lodash

Now in order to use Lodash, you need to require the code file.

const _ = require("lodash");

Now let’s understand how to use Lodash with the help of the code example.

Example: In this example, we will find whether the given value parameter is a number or not using one of the inbuilt methods in the lodash _.isNumber() method.

index.js

Javascript




// Requiring the lodash library  
const _ = require("lodash");  
     
// Use of _.isNumber() method 
console.log(_.isNumber(100)); 
console.log(_.isNumber('GeeksforGeeks courses')); 
console.log(_.isNumber(78.43));


Step to run the program: Save the above file with index.js and open the terminal and type the following command in order to compile and run the program.

node index.js

Output:

true
false
true

Advantages of Lodash:

  • It provides us with various inbuilt functions for collections, arrays, to manipulate objects and other utilities methods which we can use directly instead of writing them from scratch.
  • It keeps our code more short and clean that makes it easier to understand and modify later.
  • We have to remember Lodash functions and that makes out the task of coding much simpler.
  • It is easier to start and learn for tech newbies.
  • Instead of writing repetitive functions, tasks can be accomplished with a single line of code.

Disadvantages of Lodash:

  • The firstmost disadvantage of Lodash is speed. Lodash methods take more time to execute than normal JavaScript functions.
  • In JavaScript, we can chain many functions together as we want but in Lodash we can’t chain functions, we can only wrap them.


Previous Article
Next Article

Similar Reads

How to use 'lodash' Package for Array Manipulation in a JavaScript Project ?
In this article, we'll look at how to use lodash for array manipulation tasks like sorting, filtering, mapping, etc. The 'lodash' is a very important and useful npm package that provides a set of functions to work with arrays, strings, objects, etc. Working with arrays is an important task in a javascript project which can be done through the 'loda
4 min read
Lodash or Underscore - pick, pickBy, omit, omitBy
Javascript is Everywhere. The Javascript is used widely and it’s not limited to just only in your web browser but also widely used in the server-side as well. JavaScript is used by 95% of all the websites. Lodash or Underscore makes coding in Javascript much easier &amp; also makes our code minimal, it works with arrays, numbers, objects, strings,
2 min read
Difference between lodash and Underscore
The lodash and UnderScore both are utility libraries from JavaScript which helps make it easier by providing utils which makes, working with arrays, numbers, objects, and strings much easier. They provide a group of tools used for common programming operations having a strong functional programming task. Lodash: It is a JavaScript utility library t
3 min read
Node.js lodash.sortBy() Function
Lodash is a module in Node.js that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The Loadsh.sortBy() function is used to sort the array in ascending order. Syntax: sortBy(collection, [iteratees=[_.identity]]) Parameters: This parameter holds the collection as a first parameter, Second parame
1 min read
Lodash _.stubFalse() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.stubFalse() method is used to always return a false value. The basic difference between using a normal false value and using the _.stubFalse() method is that lambda functions create a new different functi
1 min read
Lodash _.toArray() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.toArray() method is used to convert the given value to an array. Syntax: _.toArray( value ) Parameters: This method accepts a single parameter as mentioned above and described below: value: This parameter
1 min read
Lodash _.splitAt() Method
The Lodash _.splitAt() method takes an array and a numeric index and returns a new array containing two embedded arrays made by splitting the original array at the provided numeric index. Syntax: _.splitAt(array, numeric_index) Parameters: This method takes two parameters as mentioned above and described below: array: The array to be split.numeric_
3 min read
Lodash _.stubString()Method
The Lodash _.stubString() method is used to create an empty string. Syntax: _.stubString() Parameters: This method does not accept any parameter. Return Value: This method returns an empty string. Example 1: C/C++ Code // Requiring the lodash library const _ = require(&quot;lodash&quot;); // Use of _.stubString() method // to create an empty String
1 min read
Lodash _.curry2() Method
The Lodash _.curry2() method returns a curried version of the given function but will curry exactly two arguments, no more or less. Syntax: _.curry2( fun ) Parameters: This method takes a single parameter as listed above and discussed below. fun: This is the given function. Return Value: It returns a curried version of the function. Note: To execut
2 min read
Lodash _.at() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, lang, function, objects, numbers etc. The _.at() method creates an array of values corresponding to paths of object. Syntax: _.at(object, [paths]) Parameters: This method accepts two parameters as mentioned above and desc
2 min read
Lodash _.conformTo() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, objects, numbers etc. The _.conformsTo() method checks if object conforms to source by invoking the predicate properties of source with the corresponding property values of object and this method is equivalent to _.confor
2 min read
Lodash _.cycle() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.cycle() method is then used to build a new array containing the given number of iterations of the given array, that are string end-to-end. Thus, the new array created contains the given array number of gi
2 min read
Lodash _.cat() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.cat() method is used to concatenate zero or more arguments into one array. It can be used to concatenate multiple types of arguments, Syntax: _.cat( arg1, arg2, ..., argn ) Parameters: args: This method a
2 min read
Lodash _.chunkAll() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.chunkAll() Method is used to break the given array in to small chunks of the given size. It is similar to the _.chunk() method except that this method will never drop short chunks from the end. Syntax: _.
2 min read
Lodash _.keepindexed() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.keepIndexed() method takes an array and a function as parameters and returns a new array filled with the non-null return results of the given function acted upon the elements of the given array. Syntax: _
2 min read
Lodash _.keep() Method
The Lodash _.keep() method takes an array and a function and hence returns an array generated which keeps only true values based on the conditions of the function. Syntax: _.keep( array, function ) Parameters: This method takes two parameters as mentioned above and described below: array: The given array from which keep array is created.function: T
2 min read
Lodash _.weave() Method
The Lodash _.weave() method takes two arrays and returns an array with both arrays woven together. Note: The array returned from _.weave() method will be woven till the shortest array, rest array values will be the same. Syntax: _.weave(array1, array2) Parameters: This method takes two parameters as mentioned above and described below: array1: The
2 min read
Lodash _.second() Method
The Lodash _.second() method takes an array and an index and hence returns an array generated by taking elements from the original array starting with the second element and ending at the given index. Syntax: _.second(array, index); Parameters: This method takes two parameters as mentioned above and described below: array: The given array from the
3 min read
Lodash _.best() Method
The Lodash _.best() method takes an array and a function and generates the best suitable value from that array using the conditions of the function. Syntax: _.best(array, function) Parameters: This method accepts two parameters as mentioned above and described below: array: The given array from which the best value is calculated.function: The funct
2 min read
Lodash _.third() Method
The Lodash _.third() method takes an array and an index and hence returns an array generated by taking elements from the original array starting with the third element and ending at the given index. Syntax: _.third(array, index); Parameters: This method takes two parameters as mentioned above and described below: array: The given array from the ele
3 min read
Lodash _.constant() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc. The _.constant method is used to create a function that returns value. Syntax: _.constant(value) Parameters: This method accepts one parameter as mentioned above and described below: value: The value to return
1 min read
Lodash _.invoke() Method
Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, collection, strings, lang, function, objects, numbers etc. The _.invoke() method is invoked the method at path of object. Syntax: _.invoke(object, path, args) Parameters: This method accepts three parameters as mentioned above and described b
2 min read
Lodash _.cons() Method
The Lodash _.cons() Method is used to construct a new array by taking some element and putting it at the front of another array or element. Syntax: _.cons(element, Array_or_element); Parameters: element: It is the element that gets put in the front to construct a new Array.Array_or_element: It is the second parameter used to construct an array. Ret
2 min read
Lodash _.iterateUntil() Method
The Lodash _.iterateUntil() Method takes two functions, one is used as a result generator, other function used as a stop-check and a seed value say n and returns an array of each generated result. The operation of _.iterateUntil() method is such that the result generator is passed with the seed value at the start and each subsequent result which wi
2 min read
Lodash _.entriesIn() Method
The _.entriesIn() method is used to create an array of own and inherited enumerable string keyed-value pairs for the specified object. If object is a map or set, its entries are returned. Syntax: _.entriesIn(object) Parameters: This method accepts a parameter as mentioned above and described below: object: This parameter holds the object to query.
1 min read
Lodash _.extend() Method
The _.extend() method is like the _.assign() method except that it iterates over its own and inherited source properties. Syntax: _.extend(object, sources) Parameters: This method accepts two parameters as mentioned above and described below: object: This parameter holds the destination object.sources: This parameter holds the source objects. Retur
1 min read
Lodash _.always() Method
The Lodash _.always() method takes a value and returns a function that will always return that value. Syntax: _.always( value ); Parameters: This method takes a single parameter as listed above and discussed below. value: This method takes a value to be returned. Return Value: This method returns a function. Note: To execute the below examples, you
1 min read
Lodash _.bound() Method
The Lodash _.bound() method returns function which is a property of an object by name, bound to the object made. Syntax: _.bound( obj, function); Parameters: This method accepts two parameters as listed above and discussed below. obj: the object in which the function is defined.function: a function defined containing the returning logic. Return Val
1 min read
Lodash _.comparator() Method
The Lodash _.comparator() method takes a binary predicate-like function and returns a comparator function which can be used as a callback for _.sort() method etc. Syntax: _.comparator( function ); Parameters: This method accepts a single parameter as listed above and discussed below. function: It is a predicate like defined function. Return Value:
2 min read
Lodash _.complement() Method
The Lodash _.complement() method returns a function that reverses the sense of a given predicate-function. Syntax: _.complement( function ); Parameters: This method accepts a single parameter as listed above and discussed below. function: This parameter predicate function defined containing the returning logic. Return Value: This method returns a f
1 min read