Level Up Your JavaScript: A Brief Introduction to Lodash

AKA the best utility for working with data in JavaScript.

If you’re trying to do anything fancy with data in JavaScript, you might be tempted to build the methods yourself. And that’s completely understandable — for a lot of new programmers, that’s the first strategy that comes to mind.

But before you get into brute forcing that new function, you might want to check out Lodash. Because odds are, it already has you covered.

Lodash is amazing for all kinds of things — iterating over arrays, objects, and strings, manipulating values, you name it.

To get started, all you have to do is run npm install lodash. Then, in the file where you want to use Lodash methods, you can load the full build using const _ = require('lodash');. You can also just load whichever parts of the build you want to use; check out the docs for more detail on how to do that.

Just to give you an idea of how useful Lodash can be, let’s go through a few of the built-in methods for arrays. Remember, this is just a tiny sample of what Lodash is capable of.

  • Compact an array.

Say you have an array with hundreds of elements. You’re trying to downsize this thing, so you want to remove any falsey values — that is, elements equal to false, null, 0, "", undefined, or NaN.

With Lodash, all you need is _.compact(array);. That’s it.

  • Check if something is an array-like value.

Checking if something is an array in vanilla JavaScript is easy enough. You can just use the .isArray() method. But let’s say you still want the function to return true if the value is array-like — that is, if it has a length and isn’t a function.

For that, you can use Lodash’s _.isArrayLike(value). So, _.isArrayLike(["a", "b", c"]); will return true, but so will _.isArrayLike("123");.

  • Check if two arrays (or objects, or pretty much anything else) are equal.

It’s a huge pitfall of vanilla JavaScript — objects and arrays are passed by reference when you check if they’re equal. So for example, let’s say you have:

If you try checking a === b, JavaScript will return false. That’s because it’s comparing where the variables are stored in memory, not what they contain.

Lodash solves this problem with its enormously powerful _.isEqual() method. With Lodash, if you check _.isEqual(a, b);, it will return true.

Again, this is just a tiny fraction of the power at your fingertips with Lodash. Check it out to instantly level up your JavaScript!

Software developer and science/tech writer. Python, Ruby on Rails, JavaScript, React/Redux, Java. Fascinated by the amazing stories behind today’s tech.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store