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 Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Perform: Traversing Elements with children

Ezeikel Pemberton
Ezeikel Pemberton
19,817 Points

What's the difference between element.onclick = function; and element.onclick = function();

Don't immediately see the difference or why there is two options. I would have thought it would be the second one and when the user clicks the function fires.

1 Answer

Ognjen Jevremovic
Ognjen Jevremovic
13,028 Points

Hello Ezeikel. How are you? First of I'd like to congratulate you on your progression; seeing the number of badges and points you've achieved so far is really amazing! Keep on learning mate.

To answer your question (as someone who's still learning ins and outs of JS) the functionName() - with parenthesis at the end is the function trigger, indicating to the JavaScript interpreter in the web browser that we want to run the function (we're actually the ones calling for a function, or rather triggering it), whereas functionName - without parenthesis is the function reference, meaning that we're not calling the function on our own but rather are giving a web browser a condition (in this case "onclick") to actually run the function on it's own, whenever the condition is met.

Hope that answers your question. And don't forget, keep breaking things to find out how they work! All the best in your learning path and your future career