JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – three dots with string variable will expand

JavaScript Quiz – three dots with string variable will expand

Three dots with string

In javascript, three dots can a spread operator which expand the iterable such as array, string, etc where zero or more arguments is expected. But rest parameter is also used as function parameter. But here we are discuss about the spread syntax.

Basic Syntax of Spread

...iterableObj

For more detail about the spread syntax click here.

JavaScript Question

In this javascript quiz, find out what happen if three dot used with string variable. So does string will expand in array.

First, we declare a variable name using let keyword and assign an javascript string to name.

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.

console.log(array);

Any idea what should be the output of above code statement.

JavaScript Quiz Test

Here is the full code snippet for you to solve.

let name = "JS Startup";

let array = [...name];

console.log(array); // output => ?

Answer – [“J”,”S”, ,”S”,”t”,”a”,”r”,”t”,”u”,”p”]

Click To Run Code

Explanation

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 –

Check out our other javascript quiz – 

  1. JS Quiz – is the console log function really override
  2. JavaScript Quiz – delete work on object create method object
  3. JavaScript Quiz – null and undefined act as value in function parameter
  4. JS Quiz – Math random in if condition

Conclusion

To participate in our javascript quiz or challenges, tutorial, tips & tricks make sure to join our jsstartup newsletter. So, you can able to participate in our daily challenges & learn the javascript concept.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top
0 Shares
Share via
Copy link