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
Spread in Objects0%
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 IntermediateSpread in Objects
Object Destructuring in Depth

Spread in Objects

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

Introduced in ES2018 (ES9), the object spread operator (...) allows developers to spread own enumerable properties of an object into a new object literal. It is the gold standard for immutable state management in modern JavaScript and React architectures.


1. Syntax & Core Mechanics

The spread syntax expands the key-value pairs of an existing object into a newly declared object:

1const user = {
2 id: 42,
3 username: 'coder_raj'
4};
5 
6const profile = {
7 ...user,
8 role: 'Developer',
9 country: 'India'
10};
11 
12console.log(profile);
13// { id: 42, username: 'coder_raj', role: 'Developer', country: 'India' }

2. Immutable State Updates

In modern state management (Redux, Zustand, React useState), mutating existing state objects directly causes rendering bugs. The spread operator allows clean, immutable property updates:

1const initialSession = {
2 user: 'Sarah',
3 isAuthenticated: true,
4 theme: 'light',
5 lastLogin: '2026-03-01'
6};
7 
8// Updating theme immutably
9const updatedSession = {
10 ...initialSession,
11 theme: 'dark' // Overwrites previous 'theme' value!
12};
13 
14console.log(initialSession.theme); // 'light' (Preserved!)
15console.log(updatedSession.theme); // 'dark'

Overwriting Rule

Property order matters! Later properties overwrite earlier ones:

1const defaultOptions = { retries: 3, timeout: 5000 };
2 
3// Correct: user options override defaults
4const opts1 = { ...defaultOptions, timeout: 10000 }; // timeout: 10000
5 
6// Danger: defaults override user options!
7const opts2 = { timeout: 10000, ...defaultOptions }; // timeout: 5000!

3. Merging Multiple Objects

You can combine any number of objects into a single new object:

1const contact = { email: 'sarah@example.com', phone: '+1-555-0199' };
2const address = { city: 'Vancouver', postalCode: 'V6B' };
3const preferences = { newsletter: true };
4 
5const customerRecord = {
6 id: 'CUST-104',
7 ...contact,
8 ...address,
9 ...preferences
10};

4. Nested Object Updates (Immutability Pattern)

Because the spread operator only copies one level deep (shallow copy), nested objects must be spread explicitly to avoid mutating original nested references:

1const state = {
2 user: {
3 name: 'Alex',
4 preferences: {
5 notifications: true,
6 sound: false
7 }
8 }
9};
10 
11// Updating sound to true IMMUTABLY:
12const nextState = {
13 ...state,
14 user: {
15 ...state.user,
16 preferences: {
17 ...state.user.preferences,
18 sound: true
19 }
20 }
21};
22 
23console.log(state.user.preferences.sound); // false (Untouched!)
24console.log(nextState.user.preferences.sound); // true

5. Spreading Null, Undefined, or Primitives

Unlike functions expecting objects, spreading null or undefined inside an object literal is completely safe and evaluates to nothing (no properties added):

1const extraData = null;
2const safeObj = {
3 base: 'ok',
4 ...extraData // Completely ignored, does NOT throw an error!
5};
6 
7console.log(safeObj); // { base: 'ok' }

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the result of { a: 1, b: 2, a: 5 }?
Question 2
What happens if you spread null or undefined inside an object literal ({ ...null })?
Question 3
Why is object spread heavily favored in React and modern UI state management?
Question 4
In const res = { ...user, age: 30 }, what happens if user already contains age: 25?
Question 5
Does spreading a nested object ({ ...state }) create deep copies of its inner objects?
Next Lesson

Object Destructuring in Depth

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

Related Lessons

Previous LessonNext Lesson
Object.assign()Object Destructuring in Depth

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