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 Introducing the Practice

guram dgebuadze
seal-mask
.a{fill-rule:evenodd;}techdegree
guram dgebuadze
Front End Web Development Techdegree Student 4,122 Points

(num * num1); (num/num); (num - num1); (num + num1);

hi guys. can someone tell me why do we need parantheses in the end of the code. and also we can write * and / without any problem and why we use parantheses in these cases also. it's more easy when we speak about + but in another cases i don't get it.

var num = prompt('Type a number'); num = parseFloat(num); var num1 = prompt('Type a second number.'); num1 = parseFloat(num1) var message = '<h1> Math with the numbers ' + num + ' and ' + num1 + '.</h1>' message += num + ' + ' + num1 + ' = ' + (num + num1); message += '<br>'; message += num + ' - ' + num1 + ' = ' + num - num1; message += ' <br> '; message += num + ' * ' + num1 + ' = ' + (num * num1); document.write(message);

6 Answers

Steven Parker
Steven Parker
229,783 Points

Operators have precedence and associativity. For details, see the linked page.

But the short explanation is that some operations are done before others (like * before +), and otherwise they are performed in order (generally from left to right). So if you want things done in an order other than the standard one, you put parentheses around things you want done first.

For example:

let message = "The number is " + 4 + 2;    // "The number is 42"
let message = "The number is " + (4 + 2);  // "The number is 6"

On the first line, the operations are performed left-to right. so the first one converts 4 into a string and concatenates it onto the sentence. Then the second one does the same thing to the 2.

But on the second line, the parentheses cause the second operation to be performed first, which makes it a numeric add. Then the result is converted and concatenated onto the string by the other operator.

Steven Parker
Steven Parker
229,783 Points

Sometimes parentheses are added to make the intention of the code clear even if they are not necessary for it to function correctly.

Steven Parker
Steven Parker
229,783 Points

You are probably talking about an "up-vote", any user can add a vote.
No "best answer" has been selected yet, only the question owner has that option.

When you mark a "best answer", the background behind it will turn green, and a green check-mark will appear beside the question title in the index.