Lodash _.assign() method
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
_.assigndiffers from nativeObject.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
_.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 (useObject.assignwhen symbols matter).
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.
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" } 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.
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) 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.
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) 📋 _.assign vs Object.assign vs _.merge
| Topic | _.assign | Object.assign | _.merge |
|---|---|---|---|
| Depth | Shallow | Shallow | Recursive (plain objects & arrays) |
| Own keys | String keys only | String and symbol keys | String keys only |
| Inherited keys | Skipped (use _.assignIn) | Skipped | Skipped |
| Mutates destination | Yes | Yes | Yes |
| Typical use | Layer plain configs | Same, plus copy symbol-keyed metadata | Nested 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
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.
Nested values stay linked
Mutating source.address later will also change result.address. Use _.merge for nested cloning or structuredClone for true deep copies.
Symbol-keyed properties dropped
Use native Object.assign when you need the symbol-keyed properties—Lodash _.assign walks string keys only.
❓ FAQ
Summary
- Purpose: shallow-copy own enumerable string keys from sources onto a destination, left to right.
- Remember: destination is mutated and returned; nested values are shared; symbol keys are skipped.
- Next: Lodash _.assignIn(), _.merge(), or the official Lodash docs for _.assign.
_.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).
6 people found this page helpful
