Example 1 — _.add
const _ = require('lodash');
console.log(_.add(2, 3)); 
This page is a self-contained introduction to Lodash—a JavaScript utility library used in front-end apps, Node.js services, and React projects. You will learn what Lodash does, how to install it, and when it helps (or doesn’t).
Utility library.
npm & CDN.
Why teams use it.
Disadvantages.
Common methods.
Bundle wisely.
Lodash is a popular JavaScript utility library that provides a wide range of helper functions, simplifying work with arrays, objects, strings, numbers, and functions in JavaScript applications.
It is widely used in the development community to enhance code readability, maintainability, and overall efficiency. Instead of rewriting the same loop or null-check in every file, you call a well-tested utility like _.uniq() or _.get().
Learn plain JavaScript first. Use Lodash when a utility saves real time—not for every map or filter that native JS already handles cleanly.
To use Lodash, include it in your project via npm, a CDN, or a downloaded script file. Below are the most common setups.
If you use npm (Node.js or a bundler like Vite or webpack):
npm install lodash Load Lodash from a CDN or local file, then use _ in your scripts:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lodash Demo</title>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
<script>
console.log(_.add(2, 3));
</script>
</body>
</html> const _ = require('lodash');
const result = _.add(2, 3);
console.log(result); // 5 Import the whole library or—better for bundle size—a single method:
// Per-method import (recommended)
import debounce from 'lodash/debounce';
// Full library (simple but larger bundle)
import _ from 'lodash'; Using Lodash in JavaScript projects comes with several advantages:
Weigh these benefits against your project size, bundle budget, and whether native JavaScript already covers your needs.
Lodash brings many advantages, but consider these potential drawbacks:
_ or method names may clash with your codeEvaluate these points in the context of your specific requirements before adopting Lodash everywhere.
| Category | Example methods | Typical use |
|---|---|---|
| Array | chunk, uniq, flatten | Split, dedupe, reshape lists |
| Collection | map, filter, groupBy | Iterate arrays and objects |
| Object | get, merge, pick | Safe paths, merge, subset keys |
| Function | debounce, throttle, once | Control how often functions run |
| Lang | cloneDeep, isEqual, isEmpty | Deep copy and type checks |
| Task | Example |
|---|---|
| Add numbers | _.add(2, 3) |
| Chunk array | _.chunk([1,2,3,4], 2) |
| Unique values | _.uniq([1, 2, 2, 3]) |
| Get nested path | _.get(obj, 'a.b', default) |
| Deep clone | _.cloneDeep(obj) |
| Debounce input | _.debounce(fn, 300) |
Nine starter snippets. Use View Output to preview here, or open Try It Yourself to run Lodash in the browser editor (CDN) or match the Node examples locally.
const _ = require('lodash');
console.log(_.add(2, 3)); const _ = require('lodash');
const rows = _.chunk(['a', 'b', 'c', 'd', 'e'], 2);
console.log(rows); Splits an array into groups of the given size—useful for pagination or grid layouts.
const _ = require('lodash');
const ids = _.uniq([1, 2, 2, 3, 3, 3]);
console.log(ids); const _ = require('lodash');
const user = { profile: { name: 'Alex' } };
console.log(_.get(user, 'profile.name', 'Guest'));
console.log(_.get(user, 'profile.age', 0)); Returns the default when a path is missing—no Cannot read property errors.
const _ = require('lodash');
const original = { items: [1, 2], meta: { ok: true } };
const copy = _.cloneDeep(original);
copy.items.push(3);
console.log(original.items.length); // 2
console.log(copy.items.length); // 3 Creates a deep copy so nested changes in one object do not affect the other.
| Task | Lodash | Native JS (modern) |
|---|---|---|
| Unique array | _.uniq(arr) | [...new Set(arr)] |
| Nested path | _.get(obj, 'a.b') | obj?.a?.b |
| Deep clone | _.cloneDeep(obj) | structuredClone(obj) |
| Debounce | _.debounce(fn, ms) | Manual timer or small helper |
Add Lodash via npm, CDN, or bundler import.
Use _ globally or import specific functions.
Call utilities on arrays, objects, and strings in your logic.
Less boilerplate, fewer hand-rolled loops, more readable utilities.
Lodash simplifies JavaScript development by offering a robust set of utility functions, promoting code efficiency, and providing a consistent API.
Its modular design and performance optimizations make it a valuable tool for developers working on a wide range of projects—when chosen thoughtfully alongside modern native JavaScript features.
npm install lodash; use _ or per-method imports.lodash/method imports in front-end apps to keep bundles small.import uniq from 'lodash/uniq'_.get and defaults for optional API fieldsdebounce/throttle on search and resize handlers?.optional chaining is enough_.merge mutates the target object_ variable if your app already uses it (i18n)Lodash was created by John-David Dalton and grew out of Underscore.js, adding performance improvements and extra utilities. It is one of the most downloaded packages on npm and ships in countless production apps worldwide.
Edit any example from this page in the live Try It editor.
9 people found this page helpful