By the end of this tutorial, you’ll use Lodash’s _.noConflict() to free the global _ variable while keeping Lodash available under a local name.
01
Core Syntax
const lodash = _.noConflict()
02
Restore _
Give global _ back.
03
Keep Lodash
Use the returned reference.
04
Script Tags
Classic browser use case.
05
Underscore
Coexist with other libs.
06
Modern imports
When you skip noConflict.
Fundamentals
What Is _.noConflict()?
When Lodash loads via a <script> tag in the browser, it typically assigns itself to the global _ (and often window._). If another library—commonly Underscore.js—already uses _, the last script wins and one library breaks.
💡
Beginner tip — capture the return value once
Call const lodash = _.noConflict() in a single step. That restores the previous global _and hands you the Lodash function. Do not call _.noConflict() twice—the old tutorial pattern loses Lodash or throws after the first call.
_.noConflict() is a global-namespace fix for legacy script loading. Bundled apps that import lodash from 'lodash' usually never need it—each module gets its own binding without fighting over window._.
Foundation
📝 Syntax
Restore the previous _ and receive the Lodash function:
javascript
_.noConflict()
Syntax Rules
No arguments — noConflict takes nothing.
Return value — the Lodash function (same object that was on _).
Side effect — if global _ currently points to Lodash, it reverts to the pre-Lodash value.
Assign immediately — store the return value before any code expects global _ to be Lodash.
Scope — affects the global object (window in browsers).
javascript
// Lodash loaded; _ currently references Lodash
const lodash = _.noConflict();
// Global _ is restored (e.g. to Underscore or undefined)
lodash.map([1, 2, 3], (n) => n * 2);
// [2, 4, 6]
Cheat Sheet
⚡ Quick Reference
Task
Code pattern
Notes
Correct usage
const lodash = _.noConflict()
One call, assign return
Custom name
const _lodash = _.noConflict()
Any identifier
After noConflict
lodash.chunk(arr, 2)
Use local variable
Global _ freed
window._ restored
Other lib can use _
npm / ESM
import _ from 'lodash'
Usually skip noConflict
Isolation alt
_.runInContext()
Separate _ copy
Returns
Function
Lodash itself
Mutates
global _
Restores previous
Typical clash
Underscore
Same _ name
Category
Util
Integration
Reference
🧰 Parameters
_.noConflict() accepts no parameters—behavior is entirely about globals and the return value.
(none)No args
Invoke on the Lodash function before global _ is overwritten by another consumer.
_.noConflict()
return valueRequired capture
The Lodash function reference. Assign to lodash, _lodash, or similar.
const lodash = _.noConflict()
global _Side effect
Reverts to whatever owned _ before Lodash loaded—often Underscore or undefined.
window._ !== lodash
repeat callsSafe but pointless
Further calls still return Lodash if invoked on the saved reference, but only the first call restores global _.
lodash.noConflict()
Load order matters: include Lodash, call noConflict, then load the library that needs global _—or vice versa depending on which should own _.
Hands-On
Examples Gallery
Practical _.noConflict() patterns with copy-ready code, sample output, and interactive Try It Yourself labs.
📚 Getting Started
The correct one-line pattern and a simulated library clash.
Example 1 — Capture Lodash in one call
The safe pattern: assign the return value immediately.
javascript
// _ currently points to Lodash (script tag just loaded)
const lodash = _.noConflict();
console.log(lodash.map([1, 2, 3], (n) => n * 2));
// [2, 4, 6]
// Global _ is no longer Lodash — use the lodash variable from here on
The variable name is yours—lodash, _lodash, or myLodash. What matters is consistency across the codebase after noConflict.
Example 4 — Migrate legacy code gradually
Introduce Lodash without breaking scripts that still expect the old _.
javascript
// Old app code (unchanged) — uses global _ for something else
function legacyHelper() {
return typeof _ !== "undefined" ? _.version : "none";
}
const lodash = _.noConflict();
// New features use lodash.*
const ids = lodash.map([{ id: 1 }, { id: 2 }], "id");
// legacyHelper() still sees the restored global _
📤 Console output:
[1, 2]
How It Works
Call noConflict as early as possible after Lodash loads, then route new code through the captured variable.
🚀 Beyond the Basics
Modern bundlers and script load order.
Example 5 — npm imports usually skip noConflict
With modules, Lodash never hijacks window._ unless you assign it yourself.
javascript
import lodash from "lodash";
// No noConflict needed — lodash is a module binding
console.log(lodash.chunk([1, 2, 3, 4], 2));
// [[1, 2], [3, 4]]
// Per-method imports work too:
import map from "lodash/map";
map([1, 2, 3], (n) => n * 2);
📤 Console output:
[[1, 2], [3, 4]]
When to use which
Prefer ESM/npm for new projects. Reserve noConflict for CDN script tags, WordPress-style globals, or pages mixing several legacy libraries.
Example 6 — Script load order checklist
Correct ordering prevents a flash where _ is wrong.
javascript
<!-- 1. Library that needs global _ (e.g. Underscore) -->
<script src="underscore.js"></script>
<!-- 2. Lodash (temporarily takes over _) -->
<script src="lodash.js"></script>
<!-- 3. Immediately release _ -->
<script>
const lodash = _.noConflict();
// Underscore owns _ again; use lodash.* in your app
</script>
📤 Console output:
Underscore keeps _; your code uses lodash.
How It Works
Document this order in HTML comments or your build docs so future script additions do not re-break global _.
Compare
📋 _.noConflict vs related patterns
Topic
_.noConflict()
ES module import
_.runInContext()
Assign window._ manually
Problem solved
Global _ clash
No global needed
Isolated Lodash copy
Ad-hoc globals
Environment
Script tags
Webpack/Vite/Node
Plugins/libraries
Legacy pages
Restores old _
Yes
N/A
No
No
Keeps Lodash
Via return value
Via import
Via context fn
Via your variable
Modern default
Rare
Yes
For extensions
Avoid
🧠 How _.noConflict() Works
1
Lodash loads
Lodash stores the previous global _ in an internal variable and assigns itself to _.
Load
2
You call noConflict
If global _ still points to Lodash, it is swapped back to the saved previous value.
Restore
3
Return Lodash
The method returns the Lodash function so your code keeps full API access under a local name.
Reference
=
🛠
Peaceful coexistence
Global _ belongs to the other library; lodash.* serves your utilities.
Important
📝 Notes
Always use one call: const lodash = _.noConflict()—never the old double-call pattern.
After noConflict, global _ is not Lodash—update snippets and docs to use your local variable.
Common clash partner: Underscore.js, which also exports _.
npm/TypeScript projects typically import Lodash and never touch noConflict.
For plugin authors extending Lodash, see _.runInContext() instead of mutating global _.
Next in the series: _.noop()—a harmless placeholder function.
Wrap Up
Conclusion
_.noConflict() solves a specific problem: two libraries fighting over global _. Capture the return value once, let the other library keep _, and use Lodash through your chosen local name.
For new work, module imports are simpler. Keep noConflict in your toolkit for legacy script-tag pages and mixed-library environments.
Call _.noConflict() without storing the return value
Call it twice expecting global _ to still be Lodash
Assume _ is Lodash after noConflict in new code
Use noConflict in bundled apps that never set global _
Load Lodash after dependent code without releasing _ first
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about _.noConflict()
Use these points when integrating Lodash on legacy pages.
5
Core concepts
📦01
One call
Capture return.
Basics
🔃02
Restore _
Free global.
Mechanics
🔗03
Local lodash
Keep using API.
Usage
🗃04
Script tags
Main use case.
Context
📦05
ESM imports
Often skip it.
Modern
❓ Frequently Asked Questions
_.noConflict() restores the global _ variable to whatever it was before Lodash loaded, then returns the Lodash function itself. Capture that return value—const lodash = _.noConflict()—to keep using Lodash under a different name.
No. One call is enough. The old tutorial pattern of _.noConflict() followed by const lodash = _.noConflict() is wrong—after the first call, global _ may no longer be Lodash, so the second line can fail. Always assign the return value once.
Mostly in browser script-tag setups where another library (often Underscore.js) also uses _. With npm/ES module imports, each file gets its own binding—noConflict is rarely necessary.
It only changes the global _. Lodash still exists—you hold it in the variable returned from noConflict(), e.g. lodash.map([1,2,3], fn).
noConflict frees the global _ slot for another library. runInContext creates a separate Lodash copy without touching globals—better for plugins and isolated extensions.
Yes. const myUtils = _.noConflict() works. Pick a clear name like lodash or _lodash so teammates know what the variable holds.
Did you know?
Lodash inherited the noConflict pattern from Underscore.js and jQuery—libraries that popularized owning a short global name (_ or $) before modular JavaScript was common.