Introduction to Lodash

Avanthika Meenakshi
Frontend Weekly
Published in
2 min readSep 12, 2017

Lodash is instant productivity kit when you’re working with javascript. A programmer uses functional utilities in his lifetime as many times as he blinks. Lodash comes with handful of befitting benefits to coders.

  1. Keeps your code minimal and neat.

2. You just have to remember the lodash functions. Easier to code.

3. Even newbies can understand

Check the following loop. We initialize a variable, increment it and we are also checking a condition to loop through. Loops are indefinitely important for programmers, at the same time, optimization is important as well. The same code can be moved to lodash within no time.

The lodash way of doing the same loop ends in a line. Here is how he can minimize them and fasten his pace of coding with lodash:

In the above code, “each” takes the array as first param and a callback as next, iterates through the array.

i. fruits is the iterate,

ii. index is the index of the each element in the array and

iii. fruit denotes each element in the array which is basically fruits[i]

Now lets play around some more functions.

  1. Grouping arrays based on some parameter
Grouping array by length of string with _.groupBy

2. Finding particular object in an array and extending it:

_.find and _.extend.
Ouput of the above code

And yes, lodash can be your template engine too. The base of lodash is pulled from underscore.js, yet another powerful library(now taken-over by lodash engineers). Lodash uses embedded javascript format to render templates. Here’s a pen on how it works.

Here’s how lodash can be your template engine.

Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc. You can create composite functions, manipulate objects and arrays. To try it out, you can npm/yarn/bower install it for the backend and you can use the CDN in the frontend.

Why lodash?

I have barely explained you lodash. It has too many functional utility functions that can keep your yard green and clean. As I draft this article, lodash remains the most dependent node module in npm.js listings. It can be used in the server and browser side without any issues. When you’re not dealing with DOM manipulation, when it is all about MVC, you will think about abstraction, optimization and cleaning your code. This is where lodash jumps to play its part.

Happy coding!

--

--