Open In App

Lodash Introduction

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:




<!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




// 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:

Disadvantages of Lodash:


Article Tags :