Adding Multiple Array –
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.
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.
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 –
- JS Quiz – Is the calling function inside function are equal
- JS Quiz – What actually optional chaining operator doing with object
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.