Thanks everyone, I think I am starting to under. but in the link, there is an example
SEPTEMBER 15, 2021
/
#JAVASCRIPT
JavaScript Rest vs Spread Operator â Whatâs the Difference?
Oluwatobi Sofela
Oluwatobi Sofela
JavaScript Rest vs Spread Operator â Whatâs the Difference?
JavaScript uses three dots (...) for both the rest and spread operators. But these two operators are not the same.
The main difference between rest and spread is that the rest operator puts the rest of some specific user-supplied values into a JavaScript array. But the spread syntax expands iterables into individual elements.
For instance, consider this code that uses rest to enclose some values into an array:
// Use rest to enclose the rest of specific user-supplied values into an array:
function myBio(firstName, lastName, ...otherInfo) {
return otherInfo;
}
// Invoke myBio function while passing five arguments to its parameters:
myBio("Oluwatobi", "Sofela", "CodeSweetly", "Web Developer", "Male");
// The invocation above will return:
["CodeSweetly", "Web Developer", "Male"]
Try it on StackBlitz
In the snippet above, we used the ...otherInfo rest parameter to put "CodeSweetly", "Web Developer", and "Male" into an array.
Now, consider this example of a spread operator:
// Define a function with three parameters:
function myBio(firstName, lastName, company) {
return `${firstName} ${lastName} runs ${company}`;
}
// Use spread to expand an arrayâs items into individual arguments:
myBio(...["Oluwatobi", "Sofela", "CodeSweetly"]);
// The invocation above will return:
âOluwatobi Sofela runs CodeSweetlyâ
Try it on StackBlitz
In the snippet above, we used the spread operator (...) to spread ["Oluwatobi", "Sofela", "CodeSweetly"]âs content across myBio()âs parameters.
Donât worry if you donât understand the rest or spread operators yet. This article has got you covered!
In the following sections, we will discuss how rest and spread work in JavaScript.
So, without any further ado, letâs get started with the rest operator.
What Exactly Is the Rest Operator?
The rest operator is used to put the rest of some specific user-supplied values into a JavaScript array.
So, for instance, here is the rest syntax:
...yourValues
The three dots (...) in the snippet above symbolize the rest operator.
The text after the rest operator references the values you wish to encase inside an array. You can only use it before the last parameter in a function definition.
To understand the syntax better, letâs see how rest works with JavaScript functions.
How Does the Rest Operator Work in a Function?
In JavaScript functions, rest gets used as a prefix of the functionâs last parameter.
Hereâs an example:
// Define a function with two regular parameters and one rest parameter:
function myBio(firstName, lastName, ...otherInfo) {
return otherInfo;
}
The rest operator (...) instructs the computer to add whatever otherInfo (arguments) supplied by the user into an array. Then, assign that array to the otherInfo parameter.
As such, we call ...otherInfo a rest parameter.
Note: Arguments are optional values you may pass to a functionâs parameter through an invocator.
Hereâs another example:
// Define a function with two regular parameters and one rest parameter:
function myBio(firstName, lastName, ...otherInfo) {
return otherInfo;
}
// Invoke myBio function while passing five arguments to its parameters:
myBio("Oluwatobi", "Sofela", "CodeSweetly", "Web Developer", "Male");
// The invocation above will return:
["CodeSweetly", "Web Developer", "Male"]
Try it on StackBlitz
In the snippet above, notice that myBioâs invocation passed five arguments to the function.
In other words, "Oluwatobi" and "Sofela" got assigned to the firstName and lastName parameters.
At the same time, the rest operator added the remaining arguments ( "CodeSweetly", "Web Developer", and "Male") into an array and assigned that array to the otherInfo parameter.
Therefore, myBio() function correctly returned ["CodeSweetly", "Web Developer", "Male"] as the content of the otherInfo rest parameter.
Beware! You Cannot Use âuse strictâ Inside a Function Containing a Rest Parameter
Keep in mind that you cannot use the âuse strictâ directive inside any function containing a rest parameter, default parameter, or destructuring parameter. Otherwise, the computer will throw a syntax error.
For instance, consider this example below:
// Define a function with one rest parameter:
function printMyName(...value) {
"use strict";
return value;
}
// The definition above will return:
"Uncaught SyntaxError: Illegal 'use strict' directive in function with non-simple parameter list"
Try it on CodeSandbox
printMyName() returned a syntax error because we used the âuse strictâ directive inside a function with a rest parameter.
But suppose you need your function to be in strict mode while also using the rest parameter. In such a case, you can write the âuse strictâ directive outside the function.
Hereâs an example:
// Define a âuse strictâ directive outside your function:
"use strict";
// Define a function with one rest parameter:
function printMyName(...value) {
return value;
}
// Invoke the printMyName function while passing two arguments to its parameters:
printMyName("Oluwatobi", "Sofela");
// The invocation above will return:
["Oluwatobi", "Sofela"]
Try it on CodeSandbox
Note: Only place the âuse strictâ directive outside your function if it is okay for the entire script or enclosing scope to be in strict mode.
So now that we know how rest works in a function, we can talk about how it works in a destructuring assignment.
How the Rest Operator Works in a Destructuring Assignment
The rest operator typically gets used as a prefix of the destructuring assignmentâs last variable.
Hereâs an example:
// Define a destructuring array with two regular variables and one rest variable:
const [firstName, lastName, ...otherInfo] = [
"Oluwatobi", "Sofela", "CodeSweetly", "Web Developer", "Male"
];
// Invoke the otherInfo variable:
console.log(otherInfo);
// The invocation above will return:
["CodeSweetly", "Web Developer", "Male"]
Try it on StackBlitz
The rest operator (...) instructs the computer to add the rest of the user-supplied values into an array. Then, it assigns that array to the otherInfo variable.
As such, you may call ...otherInfo a rest variable.
Hereâs another example:
// Define a destructuring object with two regular variables and one rest variable:
const { firstName, lastName, ...otherInfo } = {
firstName: "Oluwatobi",
lastName: "Sofela",
companyName: "CodeSweetly",
profession: "Web Developer",
gender: "Male"
}
// Invoke the otherInfo variable:
console.log(otherInfo);
// The invocation above will return:
{companyName: "CodeSweetly", profession: "Web Developer", gender: "Male"}
so my confusion is
- How can we
console.log(otherInfo)
from a destructing
that is totally new thing for me.