Spread Syntax

🔞 ALL INFORMATION CLICK HERE 👈🏻👈🏻👈🏻
Spread Syntax
Sign up with email
Sign up
Sign up with Google
Sign up with GitHub
Sign up with Facebook
Asked
3 years, 7 months ago
Upvoted for the research. Still wondering why this matters though? If it's colloquially known as the "spread operator" already, I doubt anyone will misunderstand what is meant in a conversation.
– Patrick Roberts
Jul 5 '17 at 19:56
@PatrickRoberts You're correct, but I wanted to make a distinguishment. The biggest problem for me was there was not a single authoritative post that gave a sure answer to the question, and the question stemmed from why MDN had syntax while everyone else had operator. I just wanted to share my knowledge but also show the difference.
– Andrew Li
Jul 5 '17 at 19:58
@PatrickRoberts Let's say the one is about to make a career as JS grammar nazi but still doesn't know if he/she should punish people who say 'spread operator'. This answer provides a very good explanation.
– Estus Flask
Jul 5 '17 at 20:09
For me, ... is a punctuator that is used in spread syntax and rest parameters, it's neither operator nor syntax by itself. It's equivalent to other punctuators like , , ; and : which are used in named parts of the grammar (parameter lists, statements, object literals) but aren't called "operators". Also, the latest version of the spec is ECMAScript 2017 , I guess you quoted 2015 as that's where ... was introduced. Lastly, §12.5 to 12.6 mention all the operators and ... isn't one of them. Sorry, can only give you one up–vote for your efforts. ;-)
– RobG
Jul 6 '17 at 1:09
@RobG Yeah, I quoted the 6th edition because it was introduced then. I'll make sure to edit in and mention that all the operators are mentioned, or you could if you want.
– Andrew Li
Jul 6 '17 at 1:11
JPMorgan Chase Bank, N.A. Moscow, Russia
Senior Full Stack Engineer with a love for React, Node & Typescript REMOTE
Team Lead Engineering (Infrastructure)
The Remote Company No office location
Stack Overflow
Questions
Jobs
Developer Jobs Directory
Salary Calculator
Help
Mobile
Disable Responsiveness
Products
Teams
Talent
Advertising
Enterprise
Company
About
Press
Work Here
Legal
Privacy Policy
Terms of Service
Contact Us
Stack Exchange Network
Technology
Life / Arts
Culture / Recreation
Science
Other
Join Stack Overflow to learn, share knowledge, and build your career.
I've heard ... referred to both as 'spread syntax ' and 'the spread operator ', with the latter being a lot more popular. The URL of the relevant MDN documentation suggests that it was initially referred to as the spread operator but later changed to spread syntax, and MDN's list of operators doesn't mention it.
Google seems to suggest the term operator is more popular and accepted, with sites such as the Microsoft documentation and es6-features.org referring to it as such.
Which term would be the most correct in the context of ECMAScript, if any, and why? What about array destructuring assignment?
In all senses of the word, it's not one. It has been a huge misconception since it was introduced and despite popular opinion -- it's not one, and there are a few objective points to be made:
It should be mentioned that spread syntax comes in different 'flavors', used in different contexts and are commonly referred to by different names while using the same punctuator. Spread syntax is basically an umbrella term for the application of the ... punctuator, and see Felix Kling 's great answer detailing all the uses and names. More explanation about these individuals uses is given in the supplementary answer .
Semantically, in the context of ECMAScript, operators are just builtin functions that take in arguments and evaluate to a single value -- written in prefix, infix, or postfix notation and usually with symbolic names such as + or / . From Wikipedia :
Simply, an expression involving an operator is evaluated in some way, and the resulting value may be just a value (an r-value), or may be an object allowing assignment (an l-value).
For example, the + operator results in a value such as 2, which is a right-hand-side expression, and the . operator results in an object allowing assignment such as foo.bar , a left-hand-side expression.
On the surface, the ... punctuator 1 looks to be a prefix unary operator:
But the problem with that argument is that ...bar doesn't evaluate to a singular value; it spreads the iterable bar 's elements, one by one. The same goes for spread arguments:
Here, foo receives separate arguments from the iterable bar . They're separate values being passed to foo , not just one value. It doesn't fit the definition of an operator, so it isn't one.
Another point to be made is that operators are meant to be standalone and return a single value. For example:
As already mentioned, this works well. The problem arises when you try to do this:
If spread syntax were an operator, the latter would work fine because operators evaluate the expression to a single value but spread doesn't so it fails. Spread syntax and spread arguments only work in the context of arrays and function calls because those structures receive multiple values supplied by spreading array elements or arguments. Evaluating to multiple values is outside of the scope of what an operator is able to do.
The complete list of operators is listed in Clauses §12.5 through §12.15 in the ECMAScript 2015 Language Specification , the specification in which ... is introduced, which doesn't mention ... . It can also be inferred that it's not an operator. The two main cases mentioned in this answer in which spread syntax is in a production, for function calls (spread arguments) or array literals (spread syntax) are described below:
In these productions, there's a conclusion that can be made: that the spread 'operator' doesn't exist. As mentioned earlier, operators should be standalone, as in const bar = ...foo and evaluate to one single value. The syntax of the language prevents this, which means spread syntax was never meant to be standalone. It's an extension to array initializers and function calls , an extension to their grammar.
In computer science, the syntax of a computer language is the set of rules that defines the combinations of symbols that are considered to be a correctly structured document or fragment in that language.
Syntax is basically the 'form' of the language, rules that govern what is legal or not regarding how the code should look, and how the code should be written. In this case, ECMAScript's grammar specifically defines the ... punctuator to only appear in function calls and array literals as an extension -- which is a rule that defines a combination of symbols ( ...foo ) that are considered to be legal together, thus it is syntax similar to how an arrow function ( => ) is not an operator, but syntax 2 .
Calling ... an operator is a misnomer. An operator is a builtin function that takes in arguments (operands) and is in the form of prefix, infix, or postfix notation and evaluates to exactly one value . ... , while satisfying the first two conditions, does not satisfy the last. ... , instead, is syntax because it is defined specifically and explicitly in the language's grammar. Thus, 'the spread operator' is objectively more correctly referred to as 'spread syntax'.
1 The term 'punctuator' refers to punctuators in ECMAScript 2015 and later specifications. These symbols include syntax components and operators, and are punctators of the language. ... is a punctuator itself, but the term 'spread syntax' refers to the whole application of the punctuator.
2 => itself is a punctuator , just as ... but what I'm referring to specifically is arrow function syntax , the application of the => punctuator ( (…) => { … } ), just as spread syntax refers to the application of the ... punctuator.
There are other numerous uses of spread/rest syntax not covered in the main answer. They include:
A use for spread syntax, commonly referred to as rest syntax, is used for a variable number of arguments in a function's arguments . This differs from spread arguments, used to pass arguments to a function call based on an iterable's elements. For example:
Here, rest syntax is used for the function add to receive the rest of the arguments in the identifier addends . This does seem to evaluate to a singular value as addends is an array of the passed arguments, but what if we tried:
Here, bar and baz would both be assigned a value corresponding to the first and second arguments passed -- thus this doesn't always evaluate to one value. The underlying problem is that ...addends in the first example and ...[bar, baz] in the second doesn't actually evaluate to a value at all - it's just used during the operation of assigning an array of the arguments to the identifier. Thus, it's syntax to allow a variable number of arguments to a function, not an operator.
Spread syntax can also be used during array destructuring assignment and is actually referred to as a rest element in the language specification (because when using in destructuring, it gets the rest of the destructured iterable). A convincing argument can be made as this does seem like an operator:
It's used like a prefix unary operator. Here, bar evaluates to [1, 2, 3] — which is a single value. But this doesn't always happen, for example:
Here, first , second , and third evaluate to 1, 2, and 3 respectively. But ...[second, third] assigns to two identifiers, not one, and doesn't evaluate to a singular value, but two. Just like rest syntax, the underlying problem is that ...bar in the first example and ...[second, third] in the second doesn't actually evaluate to a value at all -- it's just used during the operation of assignment. Thus, it isn't an operator at all 2 , just new sytax to aid in unpacking values.
A final use for spread syntax is in object literals, commonly referred to as 'object spread properties' in which a target object's own enumerable properties are spread to another, for example:
This isn't an operator, just like array spread syntax isn't an operator. The concept is the same, instead of indices and elements in arrays, bar 's enumerable keys and values are spread to foo . Here, a collection of bar 's properties is spread -- not just one single value, thus it does not fit the definition of an operator.
1 Object rest/spread properties are currently in the Stage 3 proposal for ECMAScript, and will very likely be added in the near future
2 Another problem with destructuring assignment being an operator, aside from semantics, is that the language specification defines it as supplemental syntax -- not a supplemental operator , and rightfully so. It’s not standalone, as this won’t work:
It’s contextual, only allowed in, by the language’s grammar, object literals and array literals that are left-hand-side expressions. It’s also grammar that refines the interpretation of a left-hand-side expression . Again, this is an extension to add new syntax to the language, a refinement to existing grammar. That reaffirms the argument with specification.
By clicking “Post Your Answer”, you agree to our terms of service , privacy policy and cookie policy
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa . rev 2021.2.3.38486
Spread syntax - JavaScript | MDN
javascript - Is it spread " syntax " or the spread ... - Stack Overflow
How to Use the Spread Operator (…) in JavaScript | by Dr. Derek... | Medium
Rest parameters and spread syntax
JavaScript ES6— The Spread Syntax (…) | by Brandon Morelli | codeburst
How to Use the Spread Operator (…) in JavaScript
Newer versions of JavaScript have brought vast improvements to the language in terms of expressiveness and ease of…
Spread operator allows an iterable to expand in places where 0+ arguments are expected. It is mostly used in variable…
“Expand” your JavaScript knowledge with the Spread Syntax
Thanks to ES6 and the likes of Babel, writing JavaScript has become incredibly dynamic, from new language syntax to…
The evolution of JavaScript to ES6 version has brought a whole array of new tools and utilities. These tools allow us…
🤓 The physical therapist who writes JavaScript 💪 Web Developer 😎 Mentor 🧠 DPT 😄 SEO Expert 😁 React 😆 Jamstack 💬 Ask me anything 👉 DoctorDerek.com 👈
Laid-back programming + relaxing photography
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’ an iterable object arr into the list of arguments.” — JavaScript.info
The spread operator was added to JavaScript in ES6 (ES2015), just like the rest parameters , which have the same syntax: three magic dots … .
“Spread operator to the rescue! It looks similar to rest parameters, also using ... , but does quite the opposite.” — JavaScript.info
Take trying to find the largest number in an array with Math.max() :
Trying to pass an array to a JavaScript function expecting separate arguments does not work. In this case it produces a NaN result. Enter … :
The spread syntax “spreads” the array into separate arguments.
T he … spread operator is useful for many different routine tasks in JavaScript, including the following:
In each case, the spread syntax expands an iterable object, usually an array, though it can be used on any interable, including a string.
H ere are a couple basic examples of using … in JavaScript, where I demonstrate copying an array, splitting a string into characters, and combining the properties of two JavaScript objects:
In the next section, I explore each of the above uses of the spread syntax.
U sing the … spread operator is a convenient way to copy an array or combine arrays, and it can even add new items:
As seen in the last example, the spread operator can quickly combine two arrays, an operation known as array concatenation :
“The Math object's set of functions are a perfect example of the spread operator as the only argument to a function.” — @davidwalshblog on his blog
O ne of the best ways to understand the use of spread operator in JavaScript is to look at the the built-in functions Math.min() and Math.max() , which both expect a list of arguments, not an array.
S ince the spread operator “spreads” an array into different arguments, any functions that accepts multiple any number of arguments can benefit from use of the spread operator.
As noted in the last example, the spread operator can add an item to an another array with a natural, easy-to-understand syntax:
A dding an item to an array in React state is easily accomplished using the spread operator. Take the following example adapted from my article on how to add to an array in React State :
T he spread syntax is useful for combining the properties and methods on objects into a new object:
T he spread operator can convert NodeList and arguments objects to arrays, such as when selecting HTML elements on the page:
W hen programming to support Internet Explorer and browsers on older mobile devices, the spread operator is not going to work.
Here is the current browser compatibility chart:
In that case, the function Function.prototype.apply() will have the same effect as the spread syntax:
Note that the first argument to .apply() is the target for this , which in this case does not matter, so I passed in null as the first argument.
Another option would be using the tool Babel to compile the JavaScript code along with the plugin babel-plugin-transform-spread .
O ne of the benefits of using the spread operator is that it will create a new reference to its primitive values, copying them.
That means that changes to the original array will not affect the copied array, which is what would happen if the array had been linked to the original with the assignment operator = :
As you can see, the spread operator is useful for creating new instances of arrays that do not behave unexpectedly due to old references.
On the other hand, when JavaScript objects including arrays are deeply nested, the spread operator only copies the first level with a new reference, but the deeper values are still linked together.
This type of problem is called creating a deep copy, as opposed to a shallow copy. Deep copies can be made using lodash or the R.clone() method from the Ramda functional programming library.
Thanks to Michał Bargiel for pointing out that the spread operator makes a shallow copy but not a deep copy.
“The spread operator can expand another item by split an iterable element like a string or an array into individual elements:” — CodinGame .com
T he spread operator … is useful for working with arrays and objects in JavaScript. It is a convenient feature added in ES6 (ES2015).
One of my favorite uses of the spread syntax is when combining arrays such as when adding an item to React State .
I also like that it can quickly combine the properties of objects into a new object, though any properties whose names conflict will be lost.
Knowing the spread syntax definitely saves me time when coding, and I recommend using it to all JavaScript developers.
Www Massage Porn Com
Hd Mature Spread
Live Ass
Porno Sensual Adventures
Porno Group Double Penetration





















































