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
Destructuring Arrays and Objects0%
Study Materials
JavaScript for IntermediateProgress 0%
1.1Block Scope with let & const1.2Template Literals1.3Destructuring Arrays and Objects1.4Default Parameters1.5Enhanced Object Literals
HomeTutorialsJavaScript for IntermediateDestructuring Arrays and Objects
Default Parameters

Destructuring Arrays and Objects

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

Destructuring is an expressive ES6 syntax that unpacks values from arrays or properties from objects into distinct variables. It significantly reduces boilerplate code and is ubiquitous in modern JavaScript and React development.


1. Object Destructuring

Object destructuring extracts properties by their matching key names:

1const student = {
2 id: 101,
3 fullName: "Sumit Sharma",
4 city: "New Delhi",
5 role: "Instructor"
6};
7 
8// Basic destructuring:
9const { fullName, role } = student;
10console.log(fullName); // "Sumit Sharma"
11console.log(role); // "Instructor"

A. Renaming Variables During Destructuring:

Use the colon syntax (originalKey: newVariableName):

1const { fullName: studentName, city: location } = student;
2console.log(studentName); // "Sumit Sharma"
3console.log(location); // "New Delhi"

B. Default Values:

Provide fallback values if the property is undefined:

1const { score = 100, isGraduated = false } = student;
2console.log(score); // 100 (Default applied!)

C. Nested Object Destructuring:

1const user = {
2 id: 5,
3 profile: {
4 avatar: "sumit.png",
5 social: {
6 github: "sumit-msk"
7 }
8 }
9};
10 
11const { profile: { social: { github } } } = user;
12console.log(github); // "sumit-msk"

2. Array Destructuring

Array destructuring extracts elements based on their numerical position (index):

1const coordinates = [28.6139, 77.2090];
2 
3// Destructure by position:
4const [latitude, longitude] = coordinates;
5console.log(`Lat: ${latitude}, Lng: ${longitude}`);

A. Skipping Elements:

Use commas without variable names to skip unwanted positions:

1const rgb = [255, 128, 0];
2const [red, , blue] = rgb; // Skips green at index 1!
3console.log(`Red: ${red}, Blue: ${blue}`);

B. Swapping Variables Without a Temporary Variable:

One of the most elegant tricks in JavaScript:

1let a = 1;
2let b = 2;
3 
4// In-place swap using array destructuring:
5[a, b] = [b, a];
6 
7console.log(a); // 2
8console.log(b); // 1

3. Destructuring in Function Parameters (React Pattern)

In React and modern JavaScript libraries, functions destructure props directly in the parameter signature:

1// Clean parameter destructuring with default fallback:
2function renderUserBadge({ name, tier = "Standard", badgeColor = "#38bdf8" }) {
3 console.log(`[${tier.toUpperCase()}] ${name} (Color: ${badgeColor})`);
4}
5 
6renderUserBadge({ name: "Neha", tier: "Gold" });
7// [GOLD] Neha (Color: #38bdf8)

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
In object destructuring, how do you assign property name to a new variable called studentName?
Question 2
What is the value of b after: const [a, , b] = [10, 20, 30, 40];?
Question 3
How can two variables x and y be swapped in one clean line without using a third temporary variable?
Question 4
What happens if you destructure a property that does not exist on an object and has no default value?
Question 5
What is the benefit of destructuring an options object directly in function parameters?
Next Lesson

Default Parameters

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

Related Lessons

Previous LessonNext Lesson
Template LiteralsDefault Parameters

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