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 trialAJ BERRY
1,814 Pointswhen to use ( ) vs = in javascript?
hi everyone ,
i am new here. i am just a little confused about when to use ( ) vs when to use =, in JS. for eg: var Name= "john", = is used in this case. whereas, console.log( name.length), () is used in this case, why didn't we use console.log=name.length instead?
please advise, is there any rulebook i can refer to ?
2 Answers
Dane Parchment
Treehouse Moderator 11,077 PointsThe simple answer is, they do different things, that why we use them for different reasons. The equals sign is an assignment operator and we use it when we want to assign a value to something. The parantheses are used to call (execute/invoke) a function so we use it when we want to call a function.
Read below for a more detailed response
This is because the =
is an assignment operator it's job is to inform that the left side of the argument is equal to whatever is on the right side. So when you are saying var Name = 'Bob'
. What you are saying is, take this variable with the name Name
and assign it the value Bob
.
On the other hand the ()
indicates that we are calling a function. Basically when we do, console.log('Hi')
. We are actually accessing the console
object and then retrieving the log
property on it, which is actually a function. In order to invoke the function we use the ()
and add any parameters to it.
AJ BERRY
1,814 PointsThank you Dane, that is a brilliant explanation and thanks forthe super quick reply. :)