Lodash _.assign() method

Beginner
⏱️ 7 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.assign(object, ...sources) copies own enumerable string keys from each source into the destination (left to right).
  • Why it’s a shallow copy—and what that means for nested objects and arrays.
  • How _.assign differs from native Object.assign (symbol keys) and from _.merge (recursive).
  • Try each example in the editor (?tryit=1, 2, 3) with Lodash from a CDN.

Prerequisites

Comfort with plain JS objects and the difference between own and inherited keys. Read the Object methods hub for the broader vocabulary.

  • Shallow vs deep copy: nested objects are shared by reference unless you clone them explicitly.
  • Mutation awareness: the destination object is written in place and returned—callers sharing the reference will see the changes.

Overview

_.assign(object, ...sources) copies own enumerable string-keyed properties from each source onto object, walking sources left to right. The destination is mutated and also returned, so the function fits both pipeline-style and side-effecting code.

Left-to-right

Each source overwrites earlier ones on key collisions—handy for layering defaults below user overrides.

Shallow only

Nested objects and arrays are copied by reference—reach for _.merge or structuredClone when you need depth.

Tree-shakeable

Import lodash/assign when you only need this helper from the Object category.

Syntax

javascript
_.assign(object, [...sources])
  • object: destination object; mutated in place.
  • sources: zero or more source objects scanned left to right; only own enumerable string keys are copied.
  • Returns: the (now-mutated) destination object.
  • Skipped: inherited keys (use _.assignIn) and symbol-keyed properties (use Object.assign when symbols matter).
1

Basic merge with conflict resolution

Layer defaults below an override source. Sources are scanned left to right, so the rightmost value wins for any shared key.

javascript
import assign from "lodash/assign";

const defaults = { theme: "light", fontSize: 14, lang: "en" };
const overrides = { fontSize: 16, lang: "fr" };

const settings = assign({}, defaults, overrides);
// => { theme: "light", fontSize: 16, lang: "fr" }
Try it Yourself
2

Shallow copy & destination mutation

Nested values are shared by reference, and the first argument is the object that gets written into. Pass an empty object as the destination when you want isolation.

javascript
import assign from "lodash/assign";

const target = { a: 1 };
const source = { b: { c: 2 } };

const result = assign(target, source);

console.log(result === target);  // true (same reference)

source.b.c = 99;
console.log(result.b.c);         // 99  (nested object is shared)
Try it Yourself
3

Own keys only (vs assignIn)

Properties defined on a source’s prototype are skipped—_.assign only sees hasOwnProperty-true keys. Use _.assignIn (a.k.a. _.extend) when you do want inherited string keys.

javascript
import assign from "lodash/assign";

function Source() {
  this.own = "from instance";
}
Source.prototype.inherited = "from prototype";

const src = new Source();

assign({}, src);
// => { own: "from instance" }     (inherited key is skipped)
Try it Yourself

📋 _.assign vs Object.assign vs _.merge

Topic_.assignObject.assign_.merge
DepthShallowShallowRecursive (plain objects & arrays)
Own keysString keys onlyString and symbol keysString keys only
Inherited keysSkipped (use _.assignIn)SkippedSkipped
Mutates destinationYesYesYes
Typical useLayer plain configsSame, plus copy symbol-keyed metadataNested config trees

Pick _.assign for flat config layering; jump to _.merge when sources nest plain objects you want combined rather than overwritten.

Pitfalls to avoid

Mutation

Surprise writes to the destination

Callers sharing the first argument will see the new keys. Pass {} as the destination when you want a fresh object.

Depth

Nested values stay linked

Mutating source.address later will also change result.address. Use _.merge for nested cloning or structuredClone for true deep copies.

Symbols

Symbol-keyed properties dropped

Use native Object.assign when you need the symbol-keyed properties—Lodash _.assign walks string keys only.

❓ FAQ

Yes. _.assign writes properties into the first argument and returns that same reference. Pass {} as the destination when you want a fresh object: _.assign({}, defaults, overrides).
Shallow. Nested objects and arrays are copied by reference—mutating them later affects every holder. Use _.merge or structuredClone if you need a recursive copy.
Native Object.assign copies own enumerable properties including symbol-keyed ones. Lodash _.assign copies only string-keyed properties and skips symbols. Both ignore inherited keys; use _.assignIn for those.
Later sources win. Lodash walks sources left to right; for each, it copies own enumerable string keys onto the destination, overwriting any earlier values at the same key.
_.assign overwrites nested values wholesale. _.merge recursively combines plain objects and arrays, so { a: { x: 1 } } merged with { a: { y: 2 } } becomes { a: { x: 1, y: 2 } }.
Use import assign from "lodash/assign"; for ESM or const assign = require('lodash/assign') in CommonJS.

Summary

Did you know?

_.assign copies only own, enumerable, string-keyed properties—symbol keys are ignored, even though native Object.assign copies them. To pick up inherited string keys as well, reach for _.assignIn (a.k.a. _.extend).

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