Lodash
Lodash is a JavaScript library that works on the top of underscore.js. It helps in working with arrays, strings, objects, numbers, etc. It provides us with various inbuilt functions and uses a functional programming approach which that 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.
Why Lodash ?
It provides various inbuilt functions for collections, arrays, to manipulate objects, and other utility methods that we can use directly instead of writing them from scratch. It makes it easier to iterate over the arrays, strings as well as objects. Its modular methods enable the creation of composite functions easier.
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 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>
Method 2: We can install it with npm. Make sure that you have Node.js and npm installed.
npm install lodash
If you are using yarn then you can use the following command:
yarn install lodash
Now in order to use the Lodash library, you need to require it in 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 simply create an empty string using the lodash _.stubString() method.
index.js
// Requiring the lodash library const _ = require("lodash"); // Use of _.stubString() method // to create an empty String let mystring = _.stubString(); // Printing the output console.log(mystring);
Output:
"" Empty String
Learn more about Lodash.js:
- Introduction
- Array Complete Reference
- Collection Complete Reference
- Function Complete Reference
- Lang Complete Reference
- Math Complete Reference
- Object Complete Reference
- Seq Complete Reference–
- String Complete Reference
- Util Complete Reference
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above