Could someone please explain this JavaScript answer? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

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?

24th May 2017, 1:30 PM
Chris Peterkin
Chris Peterkin - avatar
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!
24th May 2017, 1:33 PM
Caleb Jore
Caleb Jore - avatar
0
Ah, I see. Thank you!
24th May 2017, 1:34 PM
Chris Peterkin
Chris Peterkin - avatar
0
john = 4 Sebastian = 9 as thy are string so 4+9 = 49 its just string function hope you get it
24th May 2017, 5:12 PM
Dhruv Saxena
Dhruv Saxena - avatar
- 2
it is using your value as a string and performing concatenation function
24th May 2017, 4:08 PM
Zeeshan Salam
Zeeshan Salam - avatar