+ 2

Help with Basic JS Question

From the javascript challenge by Gaurav Agrawal (thank you for the question) var num1 = [1,2]; var num2 = [3,4]; var set = num1 + num2; console.log(set.length); //6 Could somebody kindly explain why the output is 6? I'm fairly new to javascript and I'm really confused with this one. Thank You very much :)

27th Mar 2018, 11:05 AM
ktchpnomm
ktchpnomm - avatar
2 Answers
+ 8
When you added those arrays they were literally contatenated. so the set variable got the value of "1,23,4" whose length is 6. If you want to merge the array elements use var set = num1.concat(num2) method
27th Mar 2018, 11:47 AM
Lord Krishna
Lord Krishna - avatar
+ 1
ohh I was looking at it as another array [1,23,4] that's why I was confused of how can it be 6 instead of 3! Thank you for the answer!! :)
27th Mar 2018, 11:57 AM
ktchpnomm
ktchpnomm - avatar