+ 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 :)
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
+ 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!! :)