About Lesson
In this JavaScript lesson we are going to talk about JavaScript Spread Operator, so it is used with arrays and its syntax is exactly the same as that of Rest Operator. and it is used to split the content of an array.
Let’s create an example.
1 2 3 4 5 6 7 |
let a = [1,2,3,4,5]; let b = [6,7,8,9]; let c = [...a, ...b] console.log(c); |
Also we can use with the strings.
1 2 3 |
let hello = ['Hello', 'World']; console.log(hello); console.log(...hello); |
some time we need to convert a string in to list of characters, than we can use spread operator.
1 2 3 4 |
let name = "Parwiz"; console.log(name); console.log(...name); |
We can use spread operator with the objects.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
let obj1 = { name:'Parwiz', lastname:'Forogh' }; let obj2 = { email:'par@gmail.com', phone: 123456, ...obj1 }; console.log(obj2); |
Exercise Files
No Attachment Found