JS Startup

Challenges, Tips & Tricks & Tutorials

JavaScript Quiz – What value should return when adding multiple arrays

JavaScript Quiz – What value should return when adding multiple arrays

Adding Multiple Array –

Adding multiple arrays is not a big deal. But first, understand it javaScript array is a combination of multiple data types like string, number, boolean, float, array, object, and many more.

But when we are trying to adding multiple arrays using + arithmetic operator. Then array treated as a string means it automatically converted similar to the toString() method.

For Example, let array = [“JS”, “Startup”]; treated as “JS,Startup” during addition.

String Split Method –

String.split() method is used to split a string into an array of substrings, and returns the new array. But it doesn’t change the original string.

The split() method also pass parameter which is act as a separator string. On which basis it break string in to multiple element.

For more info about JavaScript Split Method.

JavaScript Question

In this javascript quiz code, you need to find out what happen when you add array to array.

First, we declare two variable arr & str using let keyword. Then we assign a blank array [] to arr variable & JS string to str variable.

Now, create another variable result using let which stores the result of the addition. So, now we are adding multiple arrays and then we use the split method on an str variable and add it to.

Here, you need to tell what should be the console log print.

Answer – “J,S

Click To Run Code

Explanation

Now, you know the answer is “J,S”. So, how it can be possible.

Let me explain, as i already describe you that we add multiple array using + operator then it convert array to string.

So, in first we are adding two array.

[] + [] = ”

Which gives us a empty string as nothing inside the blank array which can convert to string.

Now, in str variable first we use split method which convert a string to array by breaking into multiple element based on separator passed to it.

So, your str variable look like something.

str.split() = [‘J’, ‘S’]

When we add it with remaining statement, so finally it look like.

See how adding multiple arrays look …

” + ” + ‘J,S’

Now, you understand why the answer is ‘J,S

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 calling function inside function are equal
  2. JavaScript Quiz – Find the sum of object and variable value
  3. JS Quiz – What actually optional chaining operator doing with object
  4. JavaScript Quiz – Find value after using array shift and unshift

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