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

So confused by this course

Hey everyone,

So I'm super new to javascript and I am a little confused by some of the syntax. I started to try to start this challenge and I noticed something that I dont understand. When typing out a function for example:

function askQuestion(question){ //insert some code here }

button.onclick = function() { //insert some code here };

Ok so here is the question.... How come on the function that is attached to the "button.onclick" how come that function ends with a semicolon when the function that is not attached to that does not?

Thank you in advance!

1 Answer

Hi there,

You're correct that function definitions do not require a semicolon - that second example needs one because it is assigning the function to the button.onclick - it's just like any other assignment of a value, like a variable. As a simple example,

var animal = 'penguin';

requires a semicolon. Your second example is similar - the value being assigned is just a function instead of a string. So, if you're just defining the function by itself, you don't need a semicolon, but if you're assigning it as the value of something, you do.

Hope this helps!

Thank you! Makes more sense now.