by Sai gowtham

How to use es6 Spread(...) operator JavaScript

Spread syntax(… three dots)

The spread syntax allows us to expand the arrays or strings where zero or more arguments are needed for the function calls or array literals or object literals.

Let’s see in practice

Combine arrays

const fruits = ['apples', 'apricots', 'avocados'];
const veg = ['broccoli','corn','cucumber'];

const vegplusfruits = [...fruits,...veg];

console.log(vegplusfruits);

// ['apples', 'apricots', 'avocados','broccoli','corn','cucumber']

In the above code, we combined two arrays into a single array by using the spread operator.

Function calls

We can use spread operator to pass the array of numbers as an function arguments.

Here is an example:

function sum (a,b,c,d,e,f,g,h){
   return a+b+c+d+e+f+g+h;
}

const numbers =  [1,2,3,4,5,6,7,8];

console.log(sum(...numbers));

//output -> 36

Copying Objects

const user1 = {
    id: 1,
    name: "king",
}

const users = {
    ...user1,
    id: 2,
    name: "pop"
}

console.log(users);

// {id: 1, name: "king", id:2, name: "pop"}

Splitting the Strings

We can also use spread operator to split the single string into an array of strings.

const name = "opera";
const array = [...name];

console.log(array);

// ["o", "p", "e", "r", "a"]

Css Tutorials & Demos

How rotate an image continuously in CSS

In this demo, we are going to learn about how to rotate an image continuously using the css animations.

How to create a Instagram login Page

In this demo, i will show you how to create a instagram login page using html and css.

How to create a pulse animation in CSS

In this demo, i will show you how to create a pulse animation using css.

Creating a snowfall animation using css and JavaScript

In this demo, i will show you how to create a snow fall animation using css and JavaScript.

Top Udemy Courses

JavaScript - The Complete Guide 2021 (Beginner + Advanced)
JavaScript - The Complete Guide 2021 (Beginner + Advanced)
64,026 students enrolled
52 hours of video content
View Course
React - The Complete Guide (incl Hooks, React Router, Redux)
React - The Complete Guide (incl Hooks, React Router, Redux)
284,472 students enrolled
40 hours of video content
View Course
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
Vue - The Complete Guide (w/ Router, Vuex, Composition API)
152,857 students enrolled
48.5 hours of video content
View Course