Introductions
Destructuring, spread, and rest are powerful features of modern JavaScript. They make it easier to work with arrays, objects, and function parameters. These features may look confusing initially, but they become simple when practiced with small examples. In this chapter, you will learn how to extract values, copy data, combine arrays and objects, and handle multiple function arguments. JavaScript Destructuring, Spread and Rest practice questions with solutions help to understand the concepts.
Question 1: Destructure Values from an Array
Problem
Create an array containing a student’s name, age, and course. Use array destructuring to store these values in separate variables.
Solution
const student = ["Rahul", 18, "JavaScript"];
const [name, age, course] = student;
console.log(name);
console.log(age);
console.log(course);
Output
Rahul
18
JavaScript
Step-by-step Explanation
- The
studentarray contains three values. [name, age, course]is used for destructuring.- The first value is stored in
name. - The second value is stored in
age. - The third value is stored in
course. - Each variable can now be used separately.
Question 2: Skip an Array Value Using Destructuring
Problem
Create an array containing three numbers. Use destructuring to extract only the first and third values.
Solution
const numbers = [10, 20, 30];
const [first, , third] = numbers;
console.log(first);
console.log(third);
Output
10
30
Step-by-step Explanation
- The first value
10is stored infirst. - The empty position skips
20. - The third value
30is stored inthird. - The syntax uses a comma to skip an array element.
const [first, , third] = numbers;
The middle position is intentionally left empty.
Question 3: Destructure Properties from an Object
Problem
Create a student object containing name, age, and course. Use object destructuring to extract these properties.
Solution
const student = {
name: "Priya",
age: 19,
course: "JavaScript"
};
const { name, age, course } = student;
console.log(name);
console.log(age);
console.log(course);
Output
Priya
19
JavaScript
Step-by-step Explanation
- The
studentobject contains three properties. { name, age, course }extracts those properties.- The
nameproperty is stored inname. - The
ageproperty is stored inage. - The
courseproperty is stored incourse. - You can now use these values directly.
Question 4: Rename Variables During Object Destructuring
Problem
Create an object with name and age. Use destructuring to store them in variables named studentName and studentAge.
Solution
const student = {
name: "Aman",
age: 20
};
const {
name: studentName,
age: studentAge
} = student;
console.log(studentName);
console.log(studentAge);
Output
Aman
20
Step-by-step Explanation
The syntax:
name: studentName
means:
- Get the
nameproperty from the object. - Store its value in a variable called
studentName.
Similarly:
age: studentAge
stores the age property in studentAge.
Question 5: Copy an Array Using the Spread Operator
Problem
Create an array of numbers and use the spread operator to create a copy of the array.
Solution
const numbers = [10, 20, 30];
const copy = [...numbers];
console.log(copy);
Output
[10, 20, 30]
Step-by-step Explanation
numberscontains three values....numbersspreads those values.- The values are placed inside a new array.
copycontains the same values.- A new array is created.
The three dots ... are called the spread syntax when used to expand values.
Question 6: Combine Two Arrays Using Spread
Problem
Create two arrays of numbers and combine them into one array using the spread operator.
Solution
const firstNumbers = [10, 20, 30];
const secondNumbers = [40, 50, 60];
const allNumbers = [...firstNumbers, ...secondNumbers];
console.log(allNumbers);
Output
[10, 20, 30, 40, 50, 60]
Step-by-step Explanation
firstNumberscontains the first three values.secondNumberscontains the next three values....firstNumbersexpands the first array....secondNumbersexpands the second array.- Both sets of values are placed into
allNumbers. - The final array contains all six numbers.
Question 7: Copy and Update an Object Using Spread
Problem
Create a student object and use the spread operator to create a new object with an updated age.
Solution
const student = {
name: "Rahul",
age: 18
};
const updatedStudent = {
...student,
age: 19
};
console.log(updatedStudent);
Output
{
name: "Rahul",
age: 19
}
Step-by-step Explanation
...studentcopies the existing properties.age: 19is then added.- Because
agealready exists, its value is replaced with19. - The new object is stored in
updatedStudent. - The original
studentobject remains unchanged.
Question 8: Use Rest Parameters in a Function
Problem
Create a function that accepts any number of numbers and calculates their total.
Solution
function calculateTotal(...numbers) {
let total = 0;
for (let number of numbers) {
total += number;
}
return total;
}
console.log(calculateTotal(10, 20, 30));
Output
60
Step-by-step Explanation
...numbersis a rest parameter.- It collects all remaining arguments into an array.
calculateTotal(10, 20, 30)passes three values.- Inside the function,
numbersbecomes:
[10, 20, 30]
- The
for...ofloop processes each value. - All values are added together.
- The function returns
60.
Question 9: Use Rest with Destructuring
Problem
Create an array containing several numbers. Use destructuring to store the first number separately and collect the remaining numbers using rest syntax.
Solution
const numbers = [10, 20, 30, 40, 50];
const [first, ...remaining] = numbers;
console.log(first);
console.log(remaining);
Output
10
[20, 30, 40, 50]
Step-by-step Explanation
firstreceives the first array value....remainingcollects all remaining values.firstbecomes10.remainingbecomes:
[20, 30, 40, 50]
- This is useful when you want to separate the first value from the rest of an array.
Question 10: Combine Destructuring and Spread
Problem
Create a student object containing basic details and marks. Use destructuring to extract the student’s name and use the rest syntax to collect the remaining properties.
Solution
const student = {
name: "Neha",
age: 19,
course: "JavaScript",
marks: 90
};
const { name, ...details } = student;
console.log(name);
console.log(details);
Output
Neha
{
age: 19,
course: "JavaScript",
marks: 90
}
Step-by-step Explanation
- The
studentobject contains four properties. nameextracts thenameproperty....detailscollects all remaining properties.namebecomes"Neha".detailsbecomes a new object containingage,course, andmarks.- This technique is useful when you want to separate one property from the rest of an object.
Key Takeaways
- Destructuring extracts values from arrays or properties from objects.
- Array destructuring uses square brackets
[]. - Object destructuring uses curly brackets
{}. - You can skip array values during destructuring.
- Object properties can be renamed during destructuring.
- The spread syntax expands values from arrays or objects.
- Spread can be used to copy arrays.
- Spread can combine multiple arrays.
- Spread can copy and update objects.
- The rest syntax collects multiple values into an array or object.
- Rest parameters allow functions to accept any number of arguments.
- Rest and spread use the same
...syntax, but their purpose depends on where they are used.
FAQs
1. What is destructuring in JavaScript?
Destructuring is a convenient way to extract values from arrays or properties from objects.
const numbers = [10, 20];
const [first, second] = numbers;
console.log(first);
console.log(second);
2. What is the spread operator?
Spread syntax expands the elements of an iterable such as an array or the properties of an object.
const first = [1, 2];
const second = [3, 4];
const result = [...first, ...second];
console.log(result);
Output:
[1, 2, 3, 4]
3. What is the rest parameter?
A rest parameter collects multiple function arguments into an array.
function add(...numbers) {
console.log(numbers);
}
add(10, 20, 30);
Output:
[10, 20, 30]
4. What is the difference between spread and rest?
They use the same ... syntax but perform opposite types of operations.
Spread: expands values.
const numbers = [10, 20, 30];
console.log(...numbers);
Rest: collects values.
function show(...numbers) {
console.log(numbers);
}
5. Can I destructure an object and rename its properties?
Yes.
const user = {
name: "Rahul"
};
const { name: userName } = user;
console.log(userName);
Here, the name property is stored in userName.
6. Can spread be used to combine objects?
Yes.
const first = {
name: "Rahul"
};
const second = {
age: 18
};
const user = {
...first,
...second
};
console.log(user);
7. Can rest syntax be used with object destructuring?
Yes.
const user = {
name: "Rahul",
age: 18,
course: "JavaScript"
};
const { name, ...details } = user;
console.log(details);
The details object contains the remaining properties.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
