Difference between .extend() / .assign() and .merge() in Lodash library.

JavascriptFront End TechnologyObject Oriented Programming

assign/extend take each property in the source, copy its value as-is to destination. If property values themselves are objects, there is no recursive traversal of their properties. This is also called shallow copying/cloning. The entire object would be taken from the source and set into a destination.

Merge takes each property in the source, checks if that property is the object itself. If it then goes down recursively and tries to map child object properties from source to destination.


let _ = require('lodash');
let destination = {
   a: {
      b: 1,
      c: 2
let source = {
   a: {
      d: 2,
      c: 3
console.log(_.merge(destination, source));
console.log(_.extend(destination, source));


This will give the output −

{ a: { b: 1, c: 3, d: 2 } }
{ a: { d: 2, c: 3 } }
Published on 27-Nov-2019 10:01:12