Three dots with string
Basic Syntax of Spread
For more detail about the spread syntax click here.
let name = "JS Startup";
In the second step, we create an another variable array using let and assign an array to it in which name variable is used with three dots.
let array = [...name];
Now, we are checking the value of array variable using console log.
Any idea what should be the output of above code statement.
Here is the full code snippet for you to solve.
let name = "JS Startup"; let array = [...name]; console.log(array); // output => ?
Now, you know the answer is “[“J”,“S”, ,”S”,”t”,”a”,”r”,”t”,”u”,”i”]“. But why, what does exactly three dots or spread syntax is doing. Let me explain.
So, spread syntax expands the iterable into where zero or more argument is expected. That’s why “JS Startup” converted to “J,S,,S,t,a,r,t,u,p”.
But that expansion happen inside an array. So the final value that you get is [“J”, “S”, ” “, “S”, “t”, “a”, “r”, “t”, “u”, “p”].
At last, That’s why we get an answer “[“J”,“S”, ,”S”,”t”,”a”,”r”,”t”,”u”,”p”]“.
Let me know in the comment section, what you think about it.
I hope you understand the concept and logic behind it.
But wait, i have something more for you –
- JS Quiz – is the console log function really override
- JS Quiz – Math random in if condition
And last but not the least, don’t forget to like, comment and share. It gives us a morale boost to remain to continue. Also, join our Facebook Page
If you have any questions, please feel free to ask me in the comment section and also let me know if you have any suggestions. As suggestions are always welcome.