Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript JavaScript Basics (Retired) Working With Numbers The Random Challenge

Manuel Cano
Manuel Cano
1,058 Points

using concatenation with numbers?

So I know with string we were able to type something like this var string = ‘hello’; var string += ‘world’; and the output would be “helloworld”

it seems like you can’t do it with numbers, how exactly does that work? because I see it adds the number again next to it

var number number += parseInt(number); // if the user types 2 . then the result would be 22 not 2. So we’re unable to concatenate with integers? in this case do you just use equals to say turn it into integer and store it back in the same variable?

1 Answer

Steven Parker
Steven Parker
231,275 Points

The "+" operator does addition on two numbers. But when either operand is a string it does concatenation, and implicitly converts the other operand to a string if it is a number.

So,   2 + 2 :point_right: 4, but   "2" + 2 :point_right: "22".

Does that answer your question?

Manuel Cano
Manuel Cano
1,058 Points

It does, thank you!