JavaScript Interview Question #24
What is the spread syntax in JavaScript?
JavaScript Fundamentals, Types, Scope & Functions Junior Beginner
Quick Interview Answer
Spread syntax uses three dots to expand an iterable into individual elements or to copy enumerable properties into a new object. It is commonly used with arrays, function arguments, and object literals.
Detailed Explanation
Spread syntax uses three dots to expand an iterable into individual elements or to copy enumerable properties into a new object. It is commonly used with arrays, function arguments, and object literals.
Spread performs a shallow copy, not a deep clone. If a copied array or object contains nested objects, the nested references are still shared unless they are separately copied.
Code Example
const first = [1, 2];
const second = [...first, 3, 4];
const user = { name: "Asif", city: "Multan" };
const updated = { ...user, city: "Lahore" };
console.log(second);
console.log(updated);