Skip to content

Lodash

The utility belt that predates most of what JavaScript later added natively.

Developer UtilitiesUtilityJavaScript

What it is

Lodash is a modern JavaScript utility library delivering modularity, performance, and extras. It provides tools for working with arrays, objects, strings, functions, and more, simplifying common programming tasks.

Lodash provides hundreds of utility functions for data manipulation, functional programming, and performance optimization. Functions are available for arrays, objects, strings, numbers, and more.

Licence
MIT
Watch for
Import per-function (`lodash/debounce`) to keep the bundle small

When to use it

The question documentation cannot answer for you — because it cannot recommend something else.

Reach for it when

  • Deep operations the language still lacks — `cloneDeep`, `merge`, `get` with a path
  • `debounce` and `throttle`, which remain genuinely useful
  • An existing codebase already using it

Look elsewhere when

  • Modern JavaScript covers it — `map`, `filter`, optional chaining, `Object.groupBy` and `structuredClone` replaced much of Lodash
  • Bundle size matters and you import the whole library rather than single functions

Installation

npm install lodash

Getting started

The smallest useful thing you can do with it, and what each part means.

Using _.chunk
import _ from 'lodash';
const array = [1, 2, 3, 4, 5];
console.log(_.chunk(array, 2)); // [[1,2],[3,4],[5]]
Splits an array into chunks of the specified size.
Using _.merge
import _ from 'lodash';
const object = { a: 1 };
const other = { b: 2 };
console.log(_.merge(object, other)); // { a: 1, b: 2 }
Deep merges two objects together.

Advanced usage

Where the library earns its place over a simpler alternative.

Debounce function
import _ from 'lodash';
const log = () => console.log('Called!');
const debouncedLog = _.debounce(log, 1000);
debouncedLog(); debouncedLog(); debouncedLog();
Creates a debounced function that delays invoking `log` until after 1 second has elapsed since the last call.
Throttle function
import _ from 'lodash';
const log = () => console.log('Called!');
const throttledLog = _.throttle(log, 2000);
throttledLog(); throttledLog();
Creates a throttled function that invokes `log` at most once every 2 seconds.
Cloning objects
import _ from 'lodash';
const obj = { a: 1, b: { c: 2 } };
const clone = _.cloneDeep(obj);
console.log(clone);
Performs a deep clone of an object.
Chaining methods
import _ from 'lodash';
const result = _( [1,2,3,4] ).map(n => n * 2).filter(n => n > 4).value();
console.log(result); // [6,8]
Demonstrates chaining multiple Lodash methods to transform data.

Errors and fixes

The failures you are most likely to hit, and what actually resolves them.

Cannot read property of undefined
Ensure input objects or arrays are not undefined before using Lodash methods.
Unexpected results in deep merge
Use `_.merge` for deep merge and `_.assign` for shallow merge based on the requirement.

Best practices

  • Import only the functions you need to reduce bundle size.
  • Use chaining (`_()`) for readable sequences of operations.
  • Use debouncing and throttling for performance-sensitive event handlers.
  • Prefer Lodash for deep cloning or merging objects instead of manual implementations.
  • Leverage utility functions for safer and cleaner data manipulation.

Alternatives

Comparable options, and the reason you would pick one over the other.

Background

Why it exists, and what it was reacting to.

Lodash was created by John-David Dalton to offer a more consistent and performant alternative to native JavaScript utilities. It has become widely adopted due to its convenience, extensive documentation, and functional programming utilities.