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....
Discussions

Spread operator with objects - javascript
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 ... More on stackoverflow.com
🌐 stackoverflow.com
Spread operator not working when doing array.map on an array of objects?
It's the syntax of your callback. Change it to a.map(item => ({ ...item, someBool: true})) More on reddit.com
🌐 r/learnjavascript
3
3
October 13, 2022
Difference between map() on a spread operator array or normal array?
The first thing to understand is that the difference here isn't map vs spread. It's spread vs not spread. Both samples of code call map, and the version with the spread operator works on the result of map, not the array before map is called. Spread also has certain places where it can be used, such as within array (or object) literals and within argument lists while map simply returns and array value. Spread makes it so array values can be "spread" out as though written as individual values within the location they're being used. Beyond that, it depends on the context of where this code is placed. If you're seeing spread used like this: const myArray = [...this.state.courses.map(blabla)]; Then the spread is likely useless (an exception to be mentioned later). Spread in this kind of context can be useful for copying arrays (or other iterables) into a new array, but since map already creates a copy, it's redundant used here and the result of map alone would be fine. It may be necessary if other values are being added to the new array too, though, such as: const myArray = [...this.state.courses.map(blabla), 7, 8, 9]; This creates a new array with the contents of map as well as the values 7, 8, 9. This would be equivalent to saying this.state.courses.map(blabla).concat(7, 8, 9), though the spread syntax also let's you place the map expression anywhere within that new array configuration making it easy to add values both before and after whereas a single concat only allows you to add values after. In the case of a function call spread makes the difference of passing in an array or passing in the values of the array individually. What's needed depends on the signature of the function myFunc(...this.state.courses.map(blabla)) // all map values as individual arguments myFunc(this.state.courses.map(blabla)) // one array argument As I mentioned earlier, there is an exception to the first array literal spread example I used. I seriously doubt this would apply, one because it's uncommon in general, and two because you're code suggests something the use of something like a redux state which would mean a basic tree of plain JS objects and values. Nevertheless, it could be possible that courses is a custom array defined something like this: class EvensArray extends Array { *[Symbol.iterator]() { for (let i = 0; i < this.length; i += 2) { yield this[i]; } } } This is a custom array class which when iterated over (using spread or for..of), only returns the even indexed values. Iteration functions like forEach and map don't use the iterator behavior so they will operate on all values. So, for example, if you wanted to map over an EvensArray and only get the even values in a result array, then you could use this: const myArray = [...this.state.courses.map(blabla)]; The map will iterated over all values, performing blabla them, then the spread will iterate over and return only the even values which get assigned to a new array assigned to myArray. Though in truth, you'd want to optimize this by spreading first so your map would have to go through fewer elements... Anyway the point is, while [...array.map(fn)] is usually unnecessary and the same as array.map(fn), with a some hacking around it can actually mean something completely different. More on reddit.com
🌐 r/javascript
2
5
April 15, 2018
Spread operator
Afraid not. Javascript get syntax does not create a property that is enumerable. It can be accessed directly but won't be accessed via an object spread or a for/in loop. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get#using_getters_in_classes More on reddit.com
🌐 r/typescript
22
4
August 8, 2024
🌐
W3Schools
w3schools.com › react › react_es6_spread.asp
React ES6 Spread Operator
React Compiler React Quiz React Exercises React Syllabus React Study Plan React Server React Interview Prep React Bootcamp ... The JavaScript spread operator (...) copies all or part of an existing array or object into another ...
🌐
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
🌐
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....
🌐
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.
🌐
Medium
medium.com › @slamflipstrom › conditional-object-properties-using-spread-in-javascript-714e0a12f496
Conditional Object Properties Using Spread in JavaScript | by Sam Lindstrom | Medium
June 6, 2019 - While recently browsing Stack Overflow, ... ES6’s spread syntax. The spread operator ( ... ) allows us to expand iterables into function calls, array literals, and object literals — if you’re using ES2018....
Find elsewhere
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript tutorial › javascript object spread
JavaScript Object Spread Operator (...) - JavaScript Tutorial
November 4, 2024 - Summary: in this tutorial, you ... one. In ES6, you use the spread operator (...) to unpack elements of an array. The spread operator can be very useful to clone an array....
🌐
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 can be used to clone arrays and objects, concatenate arrays, convert a NodeList to an array, and remove duplicates from arrays. However, it should be noted that it only performs a shallow copy, which means it only copies ...
🌐
YouTube
youtube.com › deeecode the web
Spread Operator in JS | Simplified in 5 minutes - YouTube
The Spread Operator in JavaScript allows you to unroll the individual items in an iterable collection (object or array). With this operator, you can spread a...
Published   October 7, 2022
Views   2K
🌐
Appsmith
community.appsmith.com › content › guide › javascript-spread-operator-what-are-those-three-dots-my-code
The Javascript Spread Operator - What Are Those Three Dots in My Code? | Appsmith Community Portal
August 29, 2023 - The spread operator, denoted by ..., allows you to unpack the elements of an array or the properties of an object into another array or object. This means you can take an array or object, and pass it as a group of items, rather than a single item.
🌐
Medium
cleverzone.medium.com › demystifying-javascript-spread-operat-71924c888a8
Demystifying JavaScript : Spread operator (…) | by Abhijeet Kumar | Medium
August 26, 2023 - The spread operator is used to spread array values or iterables into a new array or object. It excels at tasks such as copying arrays or objects, concatenating arrays, merging objects, retrieving unique elements, passing array elements as function ...
🌐
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 ...
🌐
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.
🌐
RGB Studios
rgbstudios.org › blog › js-spread-operator
Understanding the JavaScript Spread Operator | RGB Studios
May 1, 2024 - This method offers fine-grained ... JavaScript spread operator (...) simplifies working with arrays and objects by enabling copying, merging, and spreading elements into function arguments....
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}
🌐
Stack Abuse
stackabuse.com › spread-operator-in-javascript
Spread Operator in JavaScript
August 24, 2023 - 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.
🌐
Mozilla
developer.mozilla.org › en › docs › Web › JavaScript › Reference › Operators › Spread_operator
Spread syntax (...) - JavaScript | MDN
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....
🌐
W3Schools
w3schools.com › howto › howto_js_spread_operator.asp
How To Use the Spread Operator (...) in JavaScript
The JavaScript spread operator (...) expands an iterable (like an array) into more elements.
🌐
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 - In JavaScript, spread syntax refers to the use of an ellipsis of three dots (…) to expand an iterable object into the list of arguments.