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

In JavaScript why do you place brackets on some function calls and not others? When should you do this?

I just finished the jQuery basic lesson for the form passwords and noticed when some functions were called it used a bracket at the end and other times it did not.

e.g. isPasswordValid() <--bracket $password.focus(passwordEvent) <-- no bracket

Why is this? When should you use each?

Thanks :)

calling function without brackets mean you are passing just a reference to a function & calling function with brackets means you are calling a function not passing a reference....!

suppose you have a function which is responsible for making alert on browser

 function call(){
   alert("work");
 }
call;  //no alert window because it is only reference mean it doesn't run
call(); //alert window will be display in browser because you are calling function to run

2 Answers

Aaron Loften
Aaron Loften
12,464 Points

Thats a weird but valid way to write some functions. Conditional statements can be written that way too. I know, it irks me as well. It use to be my old boss' preferred way of doing it.

Thanks Liam.

So using brackets calls a function right away, but without means at some point in future reference this function?