OneCompiler

[JavaScript] Why the output of console.log(['a', 'b'] + ['c', 'd']); is a,bc,d ?

I see the output of console.log(['a', 'b'] + ['c', 'd']); is a,bc,d This is strange why not ['a', 'b', 'c', 'd']

1 Answer

4 years ago by

+ performs a string concatenation and hence you see the output as a,bc,d. If you want to concatenate two arrays you can use spread operator as follows, which gives you the output as ['a', 'b', 'c', 'd']

console.log([...['a', 'b'] ,...['c', 'd']]);
4 years ago by Meera