• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • JavaScript

Spread syntax (three dots) in JavaScript

  • Yatendrasinh Joddha
  • March 21, 2018
  • 2 minute read
javascript
Total
0
Shares
0
0
0

Spread syntax which is used by typing three dots (…) in JavaScript. It allows an array expression or string or anything which can be iterating to be expanded in places where zero or more arguments for function calls or elements for array are expected. It can also be used for an object expression to be expanded in places where zero or more key-value pairs for object are expected.

Spread syntax can be used for:

  • Array literals or string
  • Function Calls
  • Object literals (ECMAScript 2018)

How to use Spread syntax in Array literals?

Let consider below code in which we have two arrays.

var arr1 = [4,5]
var arr2 = [1,2,3,6,7]

Now here we want to merge values of “arr1” into the “arr2” after value “3” in “arr2”. To achieve this we used to follow below traditional way.

var arr1 = [4,5]
var arr2 = [1,2,3,6,7]
arr2.splice.apply(arr2, [3, 0].concat(arr1));
console.log(arr2);

//Output: [1, 2, 3, 4, 5, 6, 7]

Here Spread of ECMAScript 2015 comes in the picture which simplifies the above code and allow us to achieve above output very easily. Consider below code:

var arr1 = [4,5]
var arr2 = [1,2,3,...arr1,6,7]
console.log(arr2);

//Output: [1, 2, 3, 4, 5, 6, 7]

Consider below experiment for easy array operations with Spread (three dots):

//Copying array
var arr = [1, 2, 3];
var arr2 = [...arr];

//Concatenate array
var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
arr1 = [...arr1, ...arr2];

How to use Spread syntax in Function?

In function if you want to pass an array as an argument you have to use Function.Prototype.apply.

Let’s consider below code:

function addition(num1,num2) {
  console.log(num1 + num2);
}
addition.apply(null,[10,20]);

//Output: 30

Using Spread above can be achieved with

function addition(num1,num2) {
  console.log(num1 + num2);
}
addition(...[10,20]);

//Output: 30

Any argument in the function can be passed using spread

function addition(num1,num2, num3, num4) {
  console.log(num1 + num2 + num3 + num4);
}
addition(5,...[10,20],5);
//Output: 40

Using spread we can pass an array to a constructor where we can’t pass simple array to call constructor with new

var date = new Date(...[2018, 03, 18]);

How to use Spread syntax with object?

Before using Spread syntax with objects make sure your browser supports ECMAScript 2018 features.

If you are doing Shallow cloning or merging of the objects Spread syntax can be useful

var res1 = { name: 'yatendra', marks1: 35 };
var res2 = { name: 'yatendra', marks2: 38 };

var cloneObj = { ...res1 };
//Output: { name: "yatendra", marks1: 35 }


var mergeObj = { ...res1, ...res2 };

//Output: { name: 'yatendra', marks1: 35, marks2: 38 }

Summary

As we have discussed Spread is used only with iterable, so if you will try below code it will throw an exception

var obj = {'key': 'value'};
var arr = [...obj]; 
//Output: TypeError: obj is not iterable

Interested in JavaScript? Click here to read more JavaScript articles in Code4Developers

Yatendrasinh Joddha
Yatendrasinh Joddha

I have healthy knowledge and experience in Azure, O365 and ASP.Net with c#, MVC, WebApi along with client-side frameworks and libraries like JavaScript, JQuery, KnockoutJs, AngularJs, Angular, ReactJs, NodeJs

Views: 55,654

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0
12 comments
  1. Pingback: arguments object in JavaScript function - Code 4 Developers
  2. Anonymous says:
    October 19, 2018 at 4:38 pm

    thanks a lot. Great post.

    Reply
  3. Anonymous says:
    November 5, 2018 at 12:29 pm

    nice post

    Reply
  4. Anonymous says:
    December 10, 2018 at 12:55 pm

    Very well explained.

    Reply
  5. Anonymous says:
    January 2, 2019 at 11:50 pm

    Thank you, finally i learn how to use this

    Reply
  6. Anonymous says:
    February 28, 2019 at 12:27 pm

    Very nice

    Reply
  7. John says:
    March 21, 2019 at 1:26 pm

    var arr1 = [4,5]
    var arr2 = [1,2,3,…arr1,6,7]
    console.log(arr2);

    that’s too easy. You manually add it after the 3.
    But let’s imagine that you have
    var arr1 = [4,5]
    var arr2 = [1,2,3,6,7]

    and now want to have an arr3 that should look like [1,2,3,4,5,6,7] without just manually put in the … after the 3.

    Reply
    1. Sarat says:
      June 21, 2019 at 7:19 pm

      you can just concatenate the two arrays and then sort the result…

      var arr1 = [4,5]
      var arr2 = [1,2,3,6,7]
      var arr3 = ([…arr1, …arr2]).sort()

      Reply
  8. Anonymous says:
    May 12, 2019 at 5:19 pm

    nice information

    Reply
  9. Anonymous says:
    May 21, 2019 at 2:26 pm

    Thank you for sharing

    Reply
  10. Anonymous says:
    January 2, 2020 at 2:39 pm

    Nice!

    Reply
  11. Pingback: Transpiling in JavaScript - Code4Developers

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • Getting Started with AWS Step Functions: Orchestration Made Simple

    Getting Started with AWS Step Functions: Orchestration Made Simple

    1 week ago
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    1 year ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    2 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

%d