Lodash _.noConflict() Method

Beginner
⏱️ 7 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
Util utilities

What You’ll Learn

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.

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._.

📝 Syntax

Restore the previous _ and receive the Lodash function:

javascript
_.noConflict()

Syntax Rules

  • No argumentsnoConflict 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]

⚡ Quick Reference

TaskCode patternNotes
Correct usageconst lodash = _.noConflict()One call, assign return
Custom nameconst _lodash = _.noConflict()Any identifier
After noConflictlodash.chunk(arr, 2)Use local variable
Global _ freedwindow._ restoredOther lib can use _
npm / ESMimport _ 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

🧰 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 value Required 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 calls Safe 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 _.

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
Try It Yourself

How It Works

Lodash saves the old global _ at load time. noConflict() puts that value back and returns Lodash so you do not lose access.

Example 2 — Lodash + another _ user

Simulate a legacy library that already owns global _.

javascript
// 1. Another library used _ first (simplified stand-in)

window._ = {

  library: "legacy",

  greet() { return "from legacy"; },

};



// 2. Lodash script loads → _ becomes Lodash temporarily



// 3. Free _ for the legacy library again

const lodash = _.noConflict();



console.log(_.greet());

console.log(lodash.sum([1, 2, 3]));

// "from legacy"

// 6
Try It Yourself

How It Works

Both libraries coexist: legacy code keeps using _; your code uses lodash. This is the classic Underscore + Lodash scenario.

📈 Practical Patterns

Naming, legacy migration, and modern alternatives.

Example 3 — Assign a descriptive variable name

Pick a name your team recognizes—avoid vague globals.

javascript
const _lodash = _.noConflict();



_lodash.forEach([1, 2, 3], (n) => console.log(n * 2));

// 2, 4, 6 logged



// Clear name: _lodash is Lodash, _ belongs to someone else
Try It Yourself

How It Works

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 _

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);

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>

How It Works

Document this order in HTML comments or your build docs so future script additions do not re-break global _.

🧠 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.

📝 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.

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.

💡 Best Practices

✅ Do

  • Assign immediately: const lodash = _.noConflict()
  • Call right after the Lodash script tag loads
  • Use a clear variable name (lodash, _lodash)
  • Document script order in HTML or README
  • Prefer ESM imports for greenfield apps

❌ Don’t

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about _.noConflict()

Use these points when integrating Lodash on legacy pages.

5
Core concepts
🔃 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.

Practice _.noConflict() in the Live Editor

Simulate a global _ clash, release it safely, and verify Lodash still works locally.

Open Try It editor →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful