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 React Basics (2018) Understanding State Handling Events

Why do we putting parenthesis onclick calls the function instantly?

I don't get it why we did not put those parentheses on the On Click call.

1 Answer

Steven Parker
Steven Parker
231,248 Points

Adding parentheses to a function name creates a call, which happens immediately. But when you provide a function as a callback, you don't want to call it now. Instead, you want to pass a reference to it so it can be called later.

And a function name by itself serves as a reference to the function without calling it.

I understand this thing @Steve but what I want to know is something else!! I want to know when we are already putting it inside the onClick then why... !! Oh got it!! inside the {} it is javascript expression and thus it gets call Yo! I got it thanks @Steve