Skip to main content

How to Spread in Javascript - ES 6

Spread syntax was introduced as part of Javascript in the ES2015/ECMAS-6.

This allows iterable likes array, object or string to get expanded.

Spread can be used in following ways:
in array : [...iterableArray, 3 , 'four', 5]
in object : {...iterableObj}

The spread operator is very useful for concat, copying,apply, expanding, math operations,etc in array and objects. Let discuss each in detail.

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


without spread
var concatenatedArray = arr1.concat(arr2); // [1,2,3,4,5,6]

with spread
var concatenatedArray = [...arr1, ...arr2 ] // [1,2,3,4,5,6]

Copying
var arr1 = [1,2,3];
var arr2 = [...arr1];
arr2.push(4);
console.log(arr2); // [1,2,3,4]
console.log(arr1); // [1,2,3]


Replacement for apply
var add = (a,b,c) => a+b+c;
var arr1 = [1,2,3];
add(...arr1); // replacing add.apply(null,arr1);


Expanding array
var programming = ["Java", "NodeJs", "Python"];
var subjects = ["Physics", ...programming, "Chemistry"];
console.log(subjects); // ["Physics", "Java", "NodeJs", "Python","Chemistry"];


Maths Operations
var arr1 = [4,6,2,3,9];
console.log(Math.min(...arr1));


Most of these works the same way on object.e.g
var obj1 = {name:"Alex",age: 27};
var obj2 = {country: "China"};
var concatenatedObj = {...obj1, ...obj2}; // {name:"Alex", age: 27, country: "China"};


Similarly we can do copying, concatenation etc for object.

Hope this article was helpful. Subscribe and leave your comment to support.
Thank you!!

Comments

Popular Posts

Come Closure in Javascript

What is a closure? If we want to derive a definition,we can say, A closure is a feature of Javascript where inner function has access to the outer (enclosing) function’s variables—scope chain. This scope chains means inner function has: access to its own scope (variables defined between its curly brackets and its parameters),access to the outer function’s variables and parameters, access to the global variables. Now we will look into it in detail. Consider following example
var globalVar = "xyz"; function outerFunc(outerArg) { var outerVar = 'Alex'; function innerFunc(innerArg) { var innerVar = 'Chris'; console.log("globalVar = " + globalVar); console.log("outerArg = " + outerArg); console.log("innerArg = " + innerArg); console.log("outerVar = " + outerVar); console.log("innerVar = " + innerVar); } innerFunc(456); } outerFunc(123); In this example, i…

Javascript Hoisting Interview Question and Answers - 5

In this post,I have tried to cover all the types of possible interview questions and answers on hoisting. If you are not familiar with the hoisting, I would highly recommend you to go to my post on hoisting. Once you are comfortable with the topic, you can attempt these questions. Understanding the type of question will help you to answer. I have provided the hint after each question. All the answers are given at the end of the post.
//Question 1 console.log('bar:', bar) bar = 15 var foo = 1 console.log(foo, bar) var bar Hint: Basics of hoisting //Question 2 var foo = 5 console.log('foo:', foo) var foo; var bar = 10; var bar; console.log('bar:', bar) var baz = 10 var baz = 12 console.log('baz:', baz) Hint: Basics of hoisting //Question 3 function foo() { function bar() { return 5 } return bar() function bar() { return 10 } } console.log(foo()); Hint: Think what happens if two functions wit…

Javascript Closure Interview Questions and Answers - 6

Here in the post, I have posted all the possible interview questions and answers on closure. If you are not familiar with the closure, I would highly recommend you to go to my post onclosure. Once you are comfortable with the topic, you can attempt these questions. All the answers are given at the end of the post.

//Question 1 function foo() { var a = 5 function bar() { console.log(a) } return bar; } var baz = foo() baz()
// Question 2 function outer(a) { var b = 2 function inner() { var c = 5 console.log(a * b * c) } return inner; } var multiply = outer(5) multiply()
// Question 3 const arr = [10, 20, 30, 40, 50] for (var i = 0; i < arr.length; i++) { setTimeout(function() { console.log('Index: ' + i + ', element: ' + arr[i]) }, 1000) }
// Question 4 for (var i = 0; i < 5; i++) { setTimeout(function(i) { return function() { console.log('The value of…