MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
Object.assign()0%
Study Materials
JavaScript for IntermediateProgress 0%
1.1Block Scope with let & const1.2Template Literals1.3Destructuring Arrays and Objects1.4Default Parameters1.5Enhanced Object Literals
6.1Object.keys, Object.values, Object.entries6.2Object.assign()6.3Spread in Objects6.4Object Destructuring in Depth
HomeTutorialsJavaScript for IntermediateObject.assign()
Spread in Objects

Object.assign()

Beginner12 min read•Updated: 2026-09-15•By Er. Sumit Kumar (MSK Institute)
Study Materials

Object.assign() copies all enumerable own properties from one or more source objects to a target object. It returns the modified target object. Prior to the ES2018 object spread operator, Object.assign() was the primary mechanism for object cloning and merging.


1. Syntax and Core Mechanics

1Object.assign(target, ...sources);
  • target: The target object to receive properties. This object is mutated in-place!
  • ...sources: One or more source objects containing properties to copy.
  • Return Value: The mutated target object.
1const target = { a: 1 };
2const source = { b: 2, c: 3 };
3 
4const returnedTarget = Object.assign(target, source);
5 
6console.log(target); // { a: 1, b: 2, c: 3 } (Mutated!)
7console.log(returnedTarget === target); // true (Same object reference!)

2. Non-Mutating Cloning & Merging

To avoid mutating existing objects, always pass an empty object {} as the first argument (target):

1const defaultSettings = {
2 theme: 'light',
3 fontSize: 14,
4 notifications: true
5};
6 
7const userCustomizations = {
8 theme: 'dark',
9 fontSize: 16
10};
11 
12// Merging safely into a new object:
13const appConfig = Object.assign({}, defaultSettings, userCustomizations);
14 
15console.log(appConfig);
16// { theme: 'dark', fontSize: 16, notifications: true }
17console.log(defaultSettings.theme); // 'light' (Untouched!)

3. Precedence and Overwriting Order

Properties are copied from left to right. Later sources overwrite earlier sources if keys collide:

1const o1 = { x: 1, y: 1 };
2const o2 = { y: 2, z: 2 };
3const o3 = { z: 3 };
4 
5const merged = Object.assign({}, o1, o2, o3);
6console.log(merged); // { x: 1, y: 2, z: 3 }

4. Object.assign() vs. Object Spread Operator ({ ...obj })

Both achieve shallow cloning and property merging, but there are important nuances:

FeatureObject.assign(target, source)Object Spread { ...source }
In-place MutationCan mutate target directly (Object.assign(existing, source))Always creates a brand-new object
Setters TriggeredInvokes setters on the target objectDoes not trigger setters on target (defines own properties)
ReadabilityFunctional method callCompact, idiomatic modern syntax
PrototypesDoes not copy prototype chainDoes not copy prototype chain
1// Functional equivalent of Object.assign({}, a, b):
2const mergedViaSpread = { ...defaultSettings, ...userCustomizations };

5. Important Limitation: Shallow Copying Only

Like the spread operator, Object.assign() does not perform deep copies:

1const base = { config: { debug: true } };
2const clone = Object.assign({}, base);
3 
4clone.config.debug = false;
5console.log(base.config.debug); // false! Both share the same nested config reference.

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What does Object.assign(target, source) do to the target object?
Question 2
How can you use Object.assign() to clone an object without mutating the original?
Question 3
If sourceA has { timeout: 1000 } and sourceB has { timeout: 5000 }, what is the timeout value in Object.assign({}, sourceA, sourceB)?
Question 4
Does Object.assign() perform a deep copy of nested objects?
Question 5
What is the modern syntactic equivalent of Object.assign({}, objA, objB)?
Next Lesson

Spread in Objects

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Object.keys, Object.values, Object.entriesSpread in Objects

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Intermediate with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext