Lodash – JavaScript Utility at its Finest

Lodash_logoYet another JavaScript library?

That’s what I thought when I first heard about it. The internet is already filled with so many useful JavaScript libraries, why bother with another one?
Well, using it changed my mind very quickly. 

What is Lodash for?

Lodash offers several functions to help you write clear and simple code. Especially when it comes to arrays or collection handling. A simple example to exclude specific elements from an array:

Many functions like this one are easy to understand while others like ‘transform’ are harder to master.

Thanks to its usefulness and small footprint, Lodash is already used in many node.js web applications. Nevertheless, you can also use it in a browser JavaScript context.

How can I use Lodash?

The answer is simple. You only need to include the single lodash.js file. You can either download it directly from the website or include the “Lodash” package using bower or NPM.

Examples

The best way to show you how many lines of code you can save while using Lodash is by using an example. So here we go!

_.difference(array, values)

This creates a new array only containing elements with values of the first array unequal to the ones given as second parameter. Notice how we only need one line when using Lodash.

_.intersection(array1, array2, …)

This creates a new array only containing elements which are included in every given array, an intersection.
In Lodash this is done with one single line of code, whereas a pure JavaScript implementation would take several lines, as you can see in the slightly modified contraption that is used by the Underscore library below.

_.flattenDeep(array)

Using this, you can easily flatten nested arrays. In pure JavaScript it’s about 10 lines of code while in Lodash it’s only one line. With flattenDeep you can flatten arrays that have any depth. The normal flatten method of Lodash is only going one level deep.

_.pluck(collection, path)

Get all the values by a key from an array. Of course, this is again a lot simpler and shorter than a pure JavaScript solution.

_.kebabCase(string)

Wait, what? Yeah, you got that one right – kebab case. The function name is written in camel case, crazy, isn’t it? Well, all it does is  to change a string to kebab case, which is, in case you didn’t know, the one which separates words by ‘’, e.g. from “kebabCase” to “kebab-case”.
Lodash offers more case-modifying functions such as camel case.

_.camelCase(string)

Conclusion

Of course there are many more functions to Lodash, but to show each one of them and their corresponding pure-JavaScript-solution (hey, kebab case!) would be just too overwhelming. If you are interested in reading more about this topic, head over to the great documentation: https://lodash.com/docs.

Compared to other libraries like underscore.js, Lodash is updated more frequently while underscore.js has some inconsistent support for arrays, strings, objects, and argument objects in newer browsers. What is more, Lodash has some more functionality, Performance Gains and 100% code coverage.

We hope that you consider using this library. If you already know underscore, the change to Lodash won’t be a problem at all. Once you’ve used Lodash, it‘s hard to ever write JavaScript applications without it again.

Sharing is caring!

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.