JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – what really spread syntax with array pushing

JavaScript Quiz – what really spread syntax with array pushing

JavaScrip Spread syntax –

JavaScript spread syntax (...) is used to expand the iterable values like an array, object, or string where one or more argument is expected.

It’s mostly used with array and object for copying one array into another array without passing a reference.

To know about more javascript spread syntax click here

JavaScript Array Push Method –

JavaScript array push method is simply a pushing method which allows inserting new elements to the array at the end of the array.

It’s syntax looks like –

Array.push(element)

This is most widely used, so make sure to learn it. For more information about the javascript array push Method click here.

JavaScript Question

Let’s come to our javascript quiz code, in which you need to find out what spread syntax with array going to push inside another array.

So first, we declare a variable arrOne using let keyword and assign an array to it.

let arrOne = [1, 2, 3];

Now, we declare an another variable arrTwo using let and assign an array to it.

let arrTwo = [4, 5, 6];

In the next step, we are using array push method with arrOne and inside it we are passing arrTwo with spread syntax.

arrOne.push(…arrTwo);

Their is a trick part also as we are now changing the first element of the arrTwo to 7

arrTwo[0] = 7;

Finally, let me in the comment section what is the output of arrOne 3rd index now.

Answer – 4

Click To Run Code

Explanation

You got the answer now which is 4. But how it can be 4.

Let me explain to you –

So, first we are pushing arrTwo variable to arrOne using spread syntax. Like this,

arrOne.push(…arrTwo);

But as we know, spread syntax expand the values of array and array push method can push multiple element if they are comma separated. So it looks like –

arrOne.push(4, 5, 6);

Now, it push all values to arrOne. In the next step, we change the value of arrTwo of 0 index to 7.

But as there is no reference is passed to arrOne so no change happens in arrOne values. So, your final arrOne looks like –

[1, 2, 3, 4, 5, 6]

That’s why we get an answer 4

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. JavaScript Quiz – Find value after using array shift and unshift
  2. JS Quiz – What value should return when adding multiple arrays
  3. JavaScript Quiz – What parseInt function return if pass string
  4. JS Quiz – What happens when add or sub null value

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
18 Shares
Share via
Copy link