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
jennifer ray
7,875 Pointsvar in Javascript
Hi,
What is the different between : var message = 'Hello World!'; vs var message = ('Hello World!'); They both display the message so should i use brackets or not or do they have no use?
4 Answers
bothxp
16,510 PointsI think that we need to be a little careful because we do some times use parentheses when setting a variable.
var test = 10 + (1 / 10);
is different to
var test = (10 + 1) / 10;
and if you try
var test2 = (10);
var test3 = 10;
then you will find that both test2 & test3 evaluate to 10.
So I think the question of why do we not use the ( ) is an interesting one and other than saying that it's just not the way you would expect to see JavaScript written I haven't been able to find a description as to why it is 'wrong'.
Roy Penrod
19,810 PointsWhen you're setting a variable in JavaScript, you do not use the parentheses. For your example, you would use:
var message = "Hello World!";
The parentheses are used when you're calling a function. For example:
console.log(message);
Peter Smith
12,347 PointsHey! In your example parentheses act like they do in in math, signifying that what is inside them should be evaluated first.
You wouldn't use them when creating a string. var message = "Hello World!" is the common way of doing it.
jennifer ray
7,875 PointsThank you so much everyone for clearing up my question :)
Roy Penrod
19,810 PointsRoy Penrod
19,810 PointsGood point, boxthp.
I didn't take into account the math situation. It probably doesn't help that I'm answering questions while working on my own project. :-)
And I'm not saying it doesn't work when you include the parentheses, just that you shouldn't do it because it's against the norm. A large part of development is making sure your code can be understood and maintained in a team environment.