New course! Every coder should learn Generative AI!
Try a free lesson0
Could someone please explain this JavaScript answer?
This was a question in a JavaScript challenge quiz: What is the output of this code? var name1 = "John"; var name2 = "Sebastian"; console.log(name1.length + "" + name2.length); I am thinking the answer would be 13...however the answer was 49. Why? Could someone please explain?
4 Answers
+ 3
The length of John is 4 and Sebastian is 9. If you add a pair of quotes like on this quiz, the whole statement becomes a string. Therefore, instead of adding 4 to 9, it concatenates them to make 49. Statement is equivalent to console.log("4" + "9");
Hope this helps!
0
Ah, I see. Thank you!
0
john = 4
Sebastian = 9
as thy are string so 4+9 = 49
its just string function hope you get it
- 2
it is using your value as a string and performing concatenation function