Well I don’t know if you can see the code but here it is:
let apple = “5”;
apple+=1;
console.log(apple);
And it is supposed to return the number “6” from there but instead it is saying the number 51. I don’t know what version of JavaScript im using because I am on code academy (im not on a challenge or tutorial rn though). Thanks in advance yall
let apple = "5"; # this returns a string
let banana = 5; # this returns an integer
apple += 1; # this concatenates "5" and "1" as strings
banana += 1; # this adds 5 and 1 as integers
2 Likes
Javascript is a little unusual in that the string concatenation operator is ‘+’ rather some separate symbol or function, which can lead to the type of error you made. With ES6, you now have string interpolation, so it’s less likely you will want or need to combine strings with ‘+’, but the danger of this type of mistake is always there in javascript.
1 Like
I wouldn’t say that JavaScript is unusual in overloading +
for string concatenation. Its used in plenty of languages, like Python and C++ for example.
2 Likes