2
  • to understand the split method I went over this link https://www.w3schools.com/jsref/jsref_split.asp
  • but not sure why comma not adding after 3 and why empty array not showing up in the output
  • is it just doing array concatenation
  • i debugged but not sure
  • can you guys let me know.
[123] + [] + 'foo'.split('');
"123f,o,o"
4
  • 1
    Why are you adding/concatenating arrays? Commented Jun 27, 2019 at 15:14
  • What you want to do? Add a comma after 123? Commented Jun 27, 2019 at 15:16
  • @epascarello hey I am trying to learn concatenation and split operations Commented Jun 27, 2019 at 15:22
  • stackoverflow.com/questions/7124884/… Commented Jun 27, 2019 at 15:24

1 Answer 1

2

When the array is converted to string. Implicitly join() is called on it. So [].join() is '' that's why it doesn't show up in string.

But if you use some empty elements then it will show ,

console.log([123] + [,] + 'foo'.split(''));

How to concat arrays:

There can be different ways to concat two or more arrays. The modern one is using Spread Operator.

console.log([...[123], ...[],...'foo'.split('')]);

Sign up to request clarification or add additional context in comments.

5 Comments

can you let me know in my code when array is converted to string
@zizi Whenever you apply + operator b/w two arrays. Arrays will be converted to a string .
hey in spread operator can you let me know why its printing in next line?
@zizi It doesn't matter. If you use the normal console it will work fine. Also its an array not a string.
hey when I give comma inside an array, can you tell me why I am getting undefined :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.