Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 1973

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T01:07:08+00:00 2024-11-26T01:07:08+00:00

Spread vs Rest Operators in JavaScript

  • 61k

Content:

  • Definition
  • Spread Operator
    • Concatenating Arrays
    • Merging Objects
    • Copying Arrays and Objects
    • Turning Strings into Arrays
  • Rest Operator
    • Function Parameters
  • Destructuring with Rest and Spread

Let's learn how to use the three dots in our code. After this article, you won't forget the difference between them! 🐱🐱🐱

Definition :

  • The spread is the operator that allows us to expand iterables into individual elements.

  • The rest is the operator we use to represent an indefinite number of arguments in an array.

Both are written using the three dots ..., but you'll see that it's easy to identify when it's a spread and when it's a rest! At the end, there's also a good tip for you!

Spread Operator

Concatenating Arrays

The spread allows us to combine two or more arrays, maintaining a concise and clean language.

Let's have some coffee? ☕️ Consider the two arrays below:

const coffee = ['coffee', 'water']; const spices = ['cinnamon', 'nutmeg', 'cardamom']; 
Enter fullscreen mode Exit fullscreen mode

We can use the spread to mix the ingredients into a single array:

const coffeeReady = [...coffee, ...spices];  console.log(coffeeReady)  // output: // ['coffee', 'water', 'cinnamon', 'nutmeg', 'cardamom']; 
Enter fullscreen mode Exit fullscreen mode

Simple! Much better than writing item by item from each of the two arrays to form coffeeReady.

Important:

1. Changes in coffee will NOT affect coffeeReady!

To better understand: when we make a copy, we can either create a new reference to the original value or just copy the value. Creating a new reference is simply creating a variable that will point to the same location in memory where the original value is.

If we had created a new reference, any changes in coffee would change coffeeReady and vice versa. But what we did with the spread was to copy only the value, which in turn will be stored in another location in memory. Thus, changes in one array will not affect the other.

However, some details could change this picture! That's because…

2. The spread only creates a shallow copy!

This means that, depending on the data contained in coffee, some changes could indeed alter coffeeReady! If coffee contained some non-primitive value, the computer would have created a reference to the values in memory. So, any change in one array would affect the other, since both would be storing a reference to the same location in memory. See below:

let a = [1, [2, 3]]; // [2, 3] is an array nested in a, and therefore // it is a non-primitive value const b = [4, 5, 6];  let c = [...a, ...b]; console.log(c); // output: [1, [2, 3], 4, 5, 6]  a[0] = 11; a[1][0] = 22;  console.log(c); // output: [1, [22, 3], 4, 5, 6] 
Enter fullscreen mode Exit fullscreen mode

See above that changing a[0] did not affect c, because we changed a primitive value. In other words, a[0] and c point to equal values, but they are in different locations in memory. However, changing a[1][0] modified c, because we changed the value to which both a[1][0] and c point.

Merging Objects

We can also merge objects into one using the spread:

const myParents = {    fathersName: 'Michael',    mothersName: 'Louise' }; const mySiblings = {    brothersName: 'Philip',    sistersName: 'Lara'  };  const myFamily = { ...myParents, ...mySiblings }; console.log(myFamily); /* output: {    fathersName: 'Michael',    mothersName: 'Louise',    brothersName: 'Philip',    sistersName: 'Lara'  }  */ 
Enter fullscreen mode Exit fullscreen mode

However, it is important to remember that the spread does not clone identical properties! Below we have two objects with brothersName:

const myParents = {    fathersName: 'Michael',    mothersName: 'Louise',   brothersName: 'Gabriel' }; const mySiblings = {    brothersName: 'Philip',    sistersName: 'Lara'  };  const myFamily = { ...myParents, ...mySiblings }; console.log(myFamily); /* output: {    fathersName: 'Michael',    mothersName: 'Louise',    brothersName: 'Philip',    sistersName: 'Lara'  } */ 
Enter fullscreen mode Exit fullscreen mode

Note that the final object does not inherit both brothersName keys. In fact, only one prevails, which is from the second object.

Copying Arrays and Objects

Got the idea so far? If we can merge arrays and also objects, that means we can also copy them individually:

// creating a shallow copy of coffee: const coffee = ['coffee', 'water']; const coffeeCopy = [...coffee]; console.log(coffeeCopy) // output:  // ['coffee', 'water'];  // creating a shallow copy of mySiblings: const mySiblings = {    brothersName: 'Philip',    sistersName: 'Lara'  };  const myFamily = {    fathersName: 'Michael',    mothersName: 'Louise',   ...mySiblings }; // Now we can treat brothersName and sistersName as  // a property of myFamily: console.log(myFamily.brothersName)  // output: Philip 
Enter fullscreen mode Exit fullscreen mode

Turning Strings into Arrays

It is also possible to use the spread to turn a string into an array. This allows us to have more flexibility when manipulating strings, since we will be able to apply array methods to strings:

const str = 'coffee'; const letters = [...str, 's.', '☕️'];  console.log(letters);// ["c", "o", "f", "f", "e", "e", "s.", "☕️"] 
Enter fullscreen mode Exit fullscreen mode

Rest Operator

As mentioned above, the rest operator is used to pack elements into an array. You'll see that the rest operator is a great ally when dealing with many values or an uncertain number of values.

Function Parameters

The rest operator allows us to represent an indefinite number of arguments as an array.

const order = function(beverage, ...otherIngredients) {   console.log(beverage);   console.log(otherIngredients); };  order('green tea', 'milk', 'brown sugar');  // output: // green tea  // ['milk', 'brown sugar'] 
Enter fullscreen mode Exit fullscreen mode

Note that this allows us to call the same function with more arguments, as the rest operator will put them all in the otherIngredients array:

const order = function(beverage, ...otherIngredients) {   console.log(beverage);   console.log(otherIngredients); };  order('green tea', 'milk', 'brown sugar', 'mint', 'tonka');  // output: // green tea  // ['milk', 'brown sugar', 'mint', 'tonka'] 
Enter fullscreen mode Exit fullscreen mode

🍵 An important detail is that the rest must be the last parameter in the function! If we wrote function(...otherIngredients, beverage) the computer wouldn't know when to stop and this code would generate an error.

Destructuring with Rest and Spread

Rest and spread are also widely used in destructuring. If you don't know what that is yet, I suggest my other two articles here: array destructuring and object destructuring.

Using rest:

const [a, b, ...others] = [1, 2, 3, 4, 5]; console.log(a, b, others);  // output: 1 2 [3, 4, 5] 
Enter fullscreen mode Exit fullscreen mode

Now, take this tip so you don't confuse rest with spread here: the rest is on the left side of = and the spread is on the right side of =.

…rest = ☕️

🍵 = …spread

Thanks for making it this far! I hope this post has helped you! Thoughts? Please, comment below!

beginnersfrontendjavascriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.