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 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
229,644 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).