Destructuring Arrays and Objects in Depth 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.
Object destructuring extracts properties by their matching key names :
1 const student = {
2 id : 101 ,
3 fullName : "Sumit Sharma" ,
4 city : "New Delhi" ,
5 role : "Instructor"
6 } ;
7
8 // Basic destructuring:
9 const { fullName , role } = student ;
10 console . log ( fullName ) ; // "Sumit Sharma"
11 console . log ( role ) ; // "Instructor"
A. Renaming Variables During Destructuring: Use the colon syntax (originalKey: newVariableName):
1 const { fullName : studentName , city : location } = student ;
2 console . log ( studentName ) ; // "Sumit Sharma"
3 console . log ( location ) ; // "New Delhi"
B. Default Values: Provide fallback values if the property is undefined:
1 const { score = 100 , isGraduated = false } = student ;
2 console . log ( score ) ; // 100 (Default applied!)
C. Nested Object Destructuring: 1 const user = {
2 id : 5 ,
3 profile : {
4 avatar : "sumit.png" ,
5 social : {
6 github : "sumit-msk"
7 }
8 }
9 } ;
10
11 const { profile : { social : { github } } } = user ;
12 console . log ( github ) ; // "sumit-msk"
Array destructuring extracts elements based on their numerical position (index) :
1 const coordinates = [ 28.6139 , 77.2090 ] ;
2
3 // Destructure by position:
4 const [ latitude , longitude ] = coordinates ;
5 console . log ( ` Lat: ${ latitude } , Lng: ${ longitude } ` ) ;
A. Skipping Elements: Use commas without variable names to skip unwanted positions:
1 const rgb = [ 255 , 128 , 0 ] ;
2 const [ red , , blue ] = rgb ; // Skips green at index 1!
3 console . log ( ` Red: ${ red } , Blue: ${ blue } ` ) ;
B. Swapping Variables Without a Temporary Variable: One of the most elegant tricks in JavaScript:
1 let a = 1 ;
2 let b = 2 ;
3
4 // In-place swap using array destructuring:
5 [ a , b ] = [ b , a ] ;
6
7 console . log ( a ) ; // 2
8 console . log ( b ) ; // 1
In React and modern JavaScript libraries, functions destructure props directly in the parameter signature:
1 // Clean parameter destructuring with default fallback:
2 function renderUserBadge ( { name , tier = "Standard" , badgeColor = "#38bdf8" } ) {
3 console . log ( ` [ ${ tier . toUpperCase ( ) } ] ${ name } (Color: ${ badgeColor } ) ` ) ;
4 }
5
6 renderUserBadge ( { name : "Neha" , tier : "Gold" } ) ;
7 // [GOLD] Neha (Color: #38bdf8)
Interactive Quiz • Knowledge Check
Practice Quiz In object destructuring, how do you assign property name to a new variable called studentName?
A const { name -> studentName } = obj;
B const { name: studentName } = obj;
C const { studentName = name } = obj;
D const [ name as studentName ] = obj;
What is the value of b after: const [a, , b] = [10, 20, 30, 40];?
A 20
B 30 (skips index 1)
C 40
D undefined
How can two variables x and y be swapped in one clean line without using a third temporary variable?
A x = y; y = x;
B [x, y] = [y, x];
C swap(x, y);
D x.swap(y);
What happens if you destructure a property that does not exist on an object and has no default value?
A It throws a ReferenceError
B The variable is initialized with undefined
C The variable is set to null
D It deletes the object
What is the benefit of destructuring an options object directly in function parameters?
A It eliminates the need for props.name repetition and documents expected object keys right in the function signature
B It prevents the function from returning promises
C It doubles execution speed
D It converts data to JSON automatically