Explore Topics

Why We Used Spread Operator and Rest Operator in JS?

Last Updated : 18 Apr, 2025 - Asked By Ashok

In what locations we can use the spread operator and rest operator, and how it works

javascript  spread operator  rest operator 

Answers
2024-06-09 18:01:28

Basically spread operators and rest operators syntax are same but serve different purposes. 

Spread operator:-The spread operator expands elements of an iterable (like an array or object) into individual elements.

Ex:- const arr1 = [1, 2, 3];
const arr2 = [...arr1, 4, 5, 6];
console.log(arr2); // Outputs: [1, 2, 3, 4, 5, 6]

Rest operator:-The rest operator collects multiple elements into a single array or object. It is often used for function parameters to handle a variable number of arguments.

Ex:-const [first, ...rest] = [1, 2, 3, 4];

console.log(first); // Outputs: 1

console.log(rest); // Outputs: [2, 3, 4

]

 

Your Answer



Other Resources

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

Q&A Image
Q&A

Ask your coding-related doubts and get answers.

Certification Image
Certification

Earn certifications to enhance your resume.

internships Image
Internships

Hands-on projects to improve your skills.

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

blog Image
Blogs

Add your technical blogs and read technical topics.

Certification Image
Certification

Earn certifications to enhance your resume.

Q&A Image
Q&A

Hands-on projects to improve your skills.