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"

1 Answers1

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('')]);
Community
  • 1
  • 1
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • can you let me know in my code `when array is converted to string` –  Jun 27 '19 at 15:21
  • @zizi Whenever you apply `+` operator b/w two arrays. Arrays will be converted to a string . – Maheer Ali Jun 27 '19 at 15:24
  • hey in spread operator can you let me know why its printing in next line? –  Jun 27 '19 at 18:19
  • @zizi It doesn't matter. If you use the normal console it will work fine. Also its an array not a string. – Maheer Ali Jun 28 '19 at 09:02
  • hey when I give comma inside an array, can you tell me why I am getting undefined :( –  Jun 28 '19 at 13:43