Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript The Solution

Timo J.
Timo J.
Courses Plus Student 20,919 Points

Why the "+" ?


I wanted to resolve this challenge but there is something I didn't get it.

Why there is the "+" symbol before and after "num2 and before the addition of (num1 + num2)?

if (num2===0) { alert(please enter another number than 0); } elseif (isNaN(num1) || isNaN(num2)) { alert(one of the two numbers is not a number please try again); } else { // build an HTML message message = "<h1>Math with the numbers " + num1 + " and " + num2 + "</h1>"; message += num1 + " + " + num2 + " = " + (num1 + num2); message += "<br>"; message += num1 + " * " + num2 + " = " + (num1 * num2); message += "<br>"; message += num1 + " / " + num2 + " = " + (num1 / num2); message += "<br>"; message += num1 + " - " + num2 + " = " + (num1 - num2);

// write message to web page document.write(message); }

3 Answers

Steven Parker
Steven Parker
218,813 Points

It's a string concatenation operator.

Most of the "+" symbols on that line are concatenation operators, joining strings together. The one inside the parentheses is a mathematical addition operator, combining the values of num1 and num2. That value is then converted into a string by implicit type coercion and then joined to the rest of the string created on that line.

Because your message will be a string you need the plus signs to concat or link together the values contained in num1 and num2 for it to be displayed in your string. As well use the plus sign to link the value after it does the addtion of (num1 + num2).