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 JavaScript Basics Working with Strings Transform and Manipulate Strings

AJ BERRY
seal-mask
.a{fill-rule:evenodd;}techdegree
AJ BERRY
Front End Web Development Techdegree Student 1,022 Points

when 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
MOD
Dane Parchment
Treehouse Moderator 10,744 Points

The 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.