Spread Syntax
๐ ALL INFORMATION CLICK HERE ๐๐ป๐๐ป๐๐ป
Spread Syntax
string = "GOOGLE" ;
console.log ( "Normal Syntax result: " + string )
console.log ( "Spread syntax result: " ) ;
console.log ( ...string ) ;
function printSum ( a,b,c,d,e ) {
console.log ( a+b+c+d+e ) ;
}
function printSum ( a, b, c, d, e ) {
console.log ( a + b + c + d + e ) ;
}
numbers = [ 1 , 2 , 3 , 4 , 5 , 6 ] ;
printSum ( ...numbers ) ;
Numbers = [ 3 , 52 , 6 , 7 , 1 , 3 , 5 , 3 , 1 , 5 , 3 , 6 , 7 , 3 , 5 , 6 , 1 , 2 ] ;
array1 = [ 2 , 3 , 4 , 5 , 6 , 6 , 7 , 7 ] ;
array2 = [ 1 , 10 , 2 , 44 , 66 , 22 , 11 , 33 , 56 ] ;
finalArray = [ ...array1, ...array2 ] ;
console.log ( finalArray ) ;
JavaScript released a lot of features with the release of the ES6 JavaScript back in 2015, one of the features that were new to JavaScript was the spread operator; the spread Operator has a similar identifier to the rest parameters which is the triple-dot โโฆโ before the variable name.
The triple-dot identifier when used before a variable name in an argument is known as the spread syntax or a spread argument. The spread argument breaks the iterable object (array or a string) and passes them on to the arguments.
To understand the difference between normal variable output and the spread variable out, take a look at the figure below:
To understand the basic functionality of the spread operator, you are simply going to create a string object and pass it inside the console.log() function. Afterward, use the spread operator to pass it inside the console.log() to examine the behavior of the spread operator:
You will get the following output when you execute the code snippet above:
The arrow is pointing at the normal string output and the red rectangle is enclosing the output of the spread syntax. You can notice gaps between each character of the spread syntax output that is because each character is being treated as a different element.
The whole purpose of the spread syntax and the spread operator is to create the spread arguments and pass them inside the function. To demonstrate this you are going to create a function that will take 5 arguments and will print the result of the values passed inside the arguments with the following lines of code:
You are going to create an array with 5 integer values inside it using the following line:
For the last step, you are going to call this function using the spread arguments and pass in the numbers array using the spread syntax like a single argument with the following line:
After executing the code, you will get the following output:
All the elements inside the numbers array were passed as individual arguments to the function and then each argument was added printed on the console.
But, What if your numbers array has more elements than the arguments of the function?; Well, in that case, only the first 5 elements of the array will be used and others will be discarded, thus, preventing any error.
To showcase this, take the above-mentioned function and pass in the following array inside it:
You will see the following result on the console:
You can see, even though we had more elements inside the array that were passed into the argumentโs list with the spread syntax, the extra elements were all discarded, and therefore, we did not get any errors.
We can even use the spread syntax or the spread operator to concatenate array elements with other elements by passing in the complete array within a single element.
You can see that the finallArray has both the array1 and array2 concatenated inside it and we only gave two elements in the โfinalArrayโ.
That is for the spread syntax in JavaScript.
Spread syntax was introduced in JavaScript as a new feature with the release of ES6. Spread syntax or commonly known as the spread argument is used to expand the elements of an iterable object -: Array or String โ over a list of arguments of a function by individually passing each element. This functionality of the ES6 JavaScript programming language allows dealing with certain scenarios where you have more arguments than the parameters of the function. The spread operator can also be used to concatenate elements of the array which we examined with the help of an example.
A Javascript Developer & Linux enthusiast with 4 years of industrial experience and proven know-how to combine creative and usability viewpoints resulting in world-class web applications. I have experience working with Vue, React & Node.js & currently working on article writing and video creation.
Linux Hint LLC, editor@linuxhint.com
1309 S Mary Ave Suite 210, Sunnyvale, CA 94087
Privacy Policy and Terms of Use
Was this page helpful?
Yes
No
Performance & security by Cloudflare
You cannot access medium.com. Refresh the page or contact the site owner to request access.
Copy and paste the Ray ID when you contact the site owner.
Ray ID:
7472368feb3316bf
7472368feb3316bf Copy
For help visit Troubleshooting guide
Upokary.com is a useful application in everyday life. It provides very handy information regarding every aspect of life. Each and every tip of upokary.com is meant to make life better.
For any kind of query or question please drop a message in the following email: upokary@gmail.com
Copyright ยฉ 2022 Upokary . All rights reserved.
The spread syntax allows an iterable (array, string) to be expanded in places.
Let say we have multiple arrays and we want to merge that to one array. We can use powerful spread syntax to merge them into one. This spread syntax can be good a replacement of concat , splice , push methods. Consider the following example:
We can use spread syntax for copying arrays. Previously we used to use slice method on arrays to copy array in JavaScript. But now in ES6 we can use spread syntax to copy array without affecting base array. See the example below:
unshift is used to insert one or more elements at the beginning of the array. Spread syntax can be used to insert element at any position of the array. If we use the spread syntax then it will insert the array elements at the beginning of the array. Let see code the code below of inserting elements at the beginning of the array before ES6 and with ES6:
Spread syntax can be used to object literals in ES6. We can use it to do a shallow copy of one object to another object. See the following code:
The spread syntax can be used to merge objects. See the following code segment:
In the above code segment the obj1 and obj2 are merged to newObj . As we can see the common properties are merged into one.
console . log ( multiply ( 1 , 2 , 3 , 4 , 5 )) ;
ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function multiplyAll ( ... args ) {
ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function welcomeSite ( siteName , bookName , ... bookTitles ) {
console . log ( ' Welcome to ' + siteName + 'โs ' + bookName + ' book' ) ; // Welcome to W3Docsโs JS book
// the rest go into bookTitles array
console . log ( bookTitles [ 0 ]) ; // Arrays
console . log ( bookTitles [ 1 ]) ; // Functions
console . log ( bookTitles . length ) ; // 2
ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
function f(arg1, ...rest, arg2) { // arg2 after ...rest!!!!
// error
}
console . log ( arguments . length ) ;
showSiteBooks ( "Javascript" , "Git" ) ;
ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
showSiteBooks ( "Javascript" , "Git" ) ;
ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
let showArgs = ( ) => console . log ( arguments [ 0 ]) ;
ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
console . log ( Math . max ( ... arr )) ; // 8 (spread makes array into a list of arguments)
ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
let arr1 = [ 2 , - 3 , 5 , 4 ] ;
let arr2 = [ 9 , 2 , - 7 , 2 ] ;
console . log ( Math . max ( ... arr1 , ... arr2 )) ; // 9
ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
let arr1 = [ 2 , - 3 , 5 , 4 ] ;
let arr2 = [ 9 , 2 , - 7 , 2 ] ;
console . log ( Math . max ( 3 , ... arr1 , 6 , ... arr2 , 18 )) ; // 18
ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
let merged = [ 1 , ... arr , 3 , ... arr2 ] ;
console . log ( merged ) ; // 1,5,4,9,3,6,8,12 (1, then arr, then 3, then arr2)
ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
console . log ([ ... str ]) ; // W,3,D,o,c,s
ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
// Array.from converts an iterable into an array
console . log ( Array . from ( str )) ; // W,3,D,o,c,s
ืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืืื
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Quizzes
HTML Basic
CSS Basic
Javascript Basic
PHP basic
ES6 Basic
TypeScript Basic
Angular Basic
React Basic
Sass Basic
Vue.js Basic
Git Basic
SQL Basic
Snippets
How To NodeJs
How To Linux
How To AngularJs
How To PHP
How To HTML
How To CSS
How To Symfony
How To Google Maps
How To Git
How To Apache
How To JavaScript
How To Java
How To Vue.js
Our Books
Learn HTML
Learn CSS
Learn Git
Learn Javascript
Learn PHP
Exercises
HTML
JavaScript
Git
CSS
PHP
Our Tools
Typing Test
Color Picker
HTML Encoder
HTML Editor
CSS Maker
Password Generator
Base 64
Code Diff
String Functions
String Length Calculator
String to MD5 Hash Generator
String to Sha256 Hash Generator
String Reverse
URL Encoder
URL Decoder
Base 64 Encoder
Base 64 Decoder
Extra Spaces Remover
String to Lowercase Converter
About Us
Privacy Policy for W3Docs
Company ยฉ W3docs. All rights reserved.
JavaScript has a range of helpful features allowing developers to work with arrays and function parameters much effortlessly. In this chapter, the rest parameters and spread syntax are covered.
The rest parameter gives you a robust way to work with an indefinite quantity of parameters.
For better understanding what rest parameter is, letโs start from the beginning.
You can call a function with an unspecified number of arguments, no matter how it can be defined.
No error can occur here, because of โenormousโ arguments. But, only the first two are counted in the output.
You can include the rest of the parameters in the function definition using three dots, like this: โฆ . It must be followed by the array name containing them. The literal meaning of the dots is โgather the remaining parameters into an array.โ
Now, letโs cover this example:
You have the option of getting the first parameters as variables gathering only the remaining part.
In the example below, the first two arguments get into variables, and the rest - into bookTitles array:
Please, take into account that the rest parameter
Football Betting Spread Meaning
Jules Jordan Oiled
Russian Ass Fuck