let original = {name: 'Joel', favoriteFood: 'oranges', age: 26};
let happyBirthday = {...original, age: 27};

happyBirthday will be an object that is identical to original but with the age overwritten.

So that would result in an object like this:

{name: 'Joel', favoriteFood: 'oranges', age: 27} // Notice the age
Answer from Pedro Filipe on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › Spread_syntax
Spread syntax (...) - JavaScript - MDN Web Docs - Mozilla
May 22, 2026 - The spread (...) syntax allows ... array literals) are expected. In an object literal, the spread syntax enumerates the properties of an object and adds the key-value pairs to the object being created....
🌐
DEV Community
dev.to › marinamosti › understanding-the-spread-operator-in-javascript-485j
Understanding the Spread Operator in JavaScript - DEV Community
September 23, 2019 - Spread syntax allows an iterable ... array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected....
🌐
W3Schools
w3schools.com › react › react_es6_spread.asp
React ES6 Spread Operator
The JavaScript spread operator (...) copies all or part of an existing array or object into another array or object. const numbersOne = [1, 2, 3]; const numbersTwo = [4, 5, 6]; const numbersCombined = [...numbersOne, ...numbersTwo]; ... The ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-spread-operator
JavaScript Spread Operator - GeeksforGeeks
The spread operator (...) in JavaScript provides a simple and expressive way to expand elements from arrays, strings, or objects. It helps make code cleaner by reducing the need for manual copying or looping.
Published   January 16, 2026
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript object spread
JavaScript Object Spread Operator (...) - JavaScript Tutorial
November 4, 2024 - The spread operator (...) can be used to merge two or more arrays into one as shown in the following example: let rgb = [ 'red', 'green', 'blue' ]; let cmyk = ['cyan', 'magenta', 'yellow', 'black']; let merge = [...rgb, ...cmyk]; console.lo...
🌐
Medium
medium.com › @pratyushpavanchoudhary › js-spread-operator-4423577f06a2
JS SPREAD (...) OPERATOR. Developers are using the JavaScript… | by Pratyush Pavan | Medium
August 26, 2024 - The spread operator (...) in JavaScript allows an iterable (such as an array or object) to be expanded into individual elements. This operator is a concise way to perform operations that would otherwise require more verbose code.
🌐
DigitalOcean
digitalocean.com › community › tutorials › js-spread-operator
What's the Spread Operator Used For in JavaScript? | DigitalOcean
July 11, 2022 - The spread operator is a feature of JavaScript introduced with ES6 that gives you access to the insides of an iterable object. An “iterable object” is anything you can iterate over item by item, such as arrays, objects literals, and strings. These kinds of JavaScript types can be traversed ...
Find elsewhere
🌐
Kinsta®
kinsta.com › home › resource center › blog › javascript tutorials › unleashing the power of javascript spread operator
Unleashing the Power of JavaScript Spread Operator - Kinsta®
October 1, 2025 - The spread operator in JavaScript is a syntax introduced in ECMAScript 6 (ES6) that allows you to spread the elements of an iterable (such as arrays, strings, or objects), into another iterable or function call.
Top answer
1 of 3
4

There are a couple of things going on there:

  • Object spread syntax (it's not an operator) allows you to "spread" the values null and undefined. When you do, no properties are added to the object being created. (This is just like Object.assign, which ignores null and undefined in its argument list.)

  • The && operator accepts two operands, evaluates the first, and if that value is falsy, takes that value as its result; it only evaluates the second operand and takes it as its result if the first value is truthy.

Taking those two things together, in the object literal, the ...(obj.size && { size: `${obj.size}cm` }) part works like this: if obj.size is undefined, it's basically ...undefined which doesn't add any properties to the object. But if obj.size is truthy, the second operand { size: `${obj.size}cm` } (an object literal) is evaluated, results in an object, and you end up with ...{ size: `${obj.size}cm` } which adds a size property to the object being created.

Note: If obj.size is 0, there will be no size property on the result, because 0 is falsy. So the author may well be incorrectly dropping size or weight with that solution:

function myFunction(obj) {
    return {
        fn: obj.fn,
        ln: obj.ln,
        ...(obj.size && { size: `${obj.size}cm` }),
        ...(obj.weight && { weight: `${obj.weight}kg` }),
    };
}

console.log(myFunction({fn: "fn", ln: "ln", size: 0}));

When obj.size is 0, you end up with ...0 in the object, which (in theory) creates a Number object (rather than primitive) from the primitive 0 then spreads its properties. It doesn't put any properties in the object because, by default, Number objects don't have any own, enumerable properties.

Now, if the problem is defined such that size and weight will never be 0, fair enough. But I would think carefully before generalizing that solution.

2 of 3
0

The lines with the spread operator should work! But they will just add values if obj.size resp. obj.weight are not undefined!

In this case they add the keys size, resp. weight to the object.

If obj.size=10, then:

{key1:value1, key2:value2 ,..., keyN:valueN, ...(obj.size && { size: `${obj.size}cm` })}
=> {key1:value1, key2:value2 ,..., keyN:valueN, ...(10 && { size: `${10}cm` })}
=> {key1:value1, key2:value2 ,..., keyN:valueN, ...({ size: `10 cm` })
=> {key1:value1, key2:value2 ,..., keyN:valueN, size: `10 cm`}

If obj.size=undefined, then:

{key1:value1, key2:value2 ,..., keyN:valueN,...(obj.size && { size: `${obj.size}cm` })}
=> {key1:value1, key2:value2 ,..., keyN:valueN,...(undefined )}
=> {key1:value1, key2:value2 ,..., keyN:valueN}
🌐
SitePoint
sitepoint.com › blog › javascript › quick tip: how to use the spread operator in javascript
Quick Tip: How to Use the Spread Operator in JavaScript — SitePoint
November 7, 2024 - The spread operator only performs a shallow copy, which means it only copies the top-level elements or properties. If an array or object contains other arrays or objects, those are copied by reference, not by value. This means that if you modify the nested arrays or objects, those changes will be reflected in the original array or object. Yes, the spread operator can be used with strings in JavaScript.
🌐
Medium
cleverzone.medium.com › demystifying-javascript-spread-operat-71924c888a8
Demystifying JavaScript : Spread operator (…) | by Abhijeet Kumar | Medium
August 26, 2023 - The spread operator, denoted by three dots (...), allows JavaScript developers to easily split array elements or object properties and spread them into a new array or object. This operator is aptly named "spread" because it enables us to spread ...
🌐
GPTWebHelper
blixtdev.com › how-to-use-the-spread-operator-in-javascript
How to Use The Spread Operator in JavaScript
December 20, 2022 - Simply put, the spread operator in JavaScript expands or unpacks the elements of an array or iterable object. ... One of the most useful operators in the JavaScript languages is also one of the least understood.
🌐
CodeSweetly
codesweetly.com › spread-operator
Spread Operator – How Spread Works in JavaScript | CodeSweetly
The spread syntax serves within array literals, function calls, and initialized properties objects to spread the values of iterable objects into separate items. So effectively, it does the opposite thing from the rest operator.
🌐
Hashnode
eleftheriabatsou.hashnode.dev › understanding-the-javascript-spread-operator-with-examples
Understanding the JavaScript Spread Operator (With Examples)
October 31, 2025 - The spread operator (...) is a convenient way to expand elements of an array or object into individual elements. It’s often used to make copies of arrays or objects, combine data, or pass multiple arguments to functions.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript array methods › javascript spread operator
The Practical Usages of JavaScript Spread Operator
May 8, 2024 - When we applied the spread operator ... is denoted by three dots (...). The spread operator unpacks elements of iterable objects such as arrays, sets, and maps into a list....
🌐
Stack Abuse
stackabuse.com › spread-operator-in-javascript
Spread Operator in JavaScript
August 24, 2023 - of JavaScript that does amazing ... statement. We can use the spread operator on iterables like a String or an array and it'll put the contents of the iterable into individual elements....
🌐
Dillion's Blog
dillionmegida.com › p › spread-operator-simplified
Spread Operator in JavaScript, Simplified - Dillion's Blog
console.log(fullInfo) // { // name: 'Micheal', // age: 100, // language: 'JavaScript', // stack: 'frontend' // } In this example, the name property of fullInfo comes after the spread info object, so the name of fulInfo would overwrite the name coming from the info object. The spread operator can also be used to spread arrays as function arguments.
🌐
Medium
eleftheriabatsou.medium.com › javascript-spread-operator-explained-with-examples-a38caf568ffd
JavaScript Spread Operator Explained (with Examples) | by Eleftheria Batsou | Medium
September 30, 2024 - The spread operator (...) is a convenient way to expand elements of an array or object into individual elements. It's often used to make copies of arrays or objects, combine data, or pass multiple arguments to functions.
🌐
Medium
medium.com › coding-at-dawn › how-to-use-the-spread-operator-in-javascript-b9e4a8b06fab
How to use the spread operator (…) in JavaScript
November 19, 2020 - Photo by Ethan Robertson on Unsplash · In JavaScript, spread syntax refers to the use of an ellipsis of three dots (…) to expand an iterable object into the list of arguments. “When ...arr is used in the function call, it ‘expands’ ...