Open In App

Lodash Introduction

Last Updated : 10 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads