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 DOM Scripting By Example Adding and Removing Names RSVP Checkbox

Colin Sandlin
Colin Sandlin
4,512 Points

Understanding the "checked" value

I'm having a bit of trouble wrapping my mind around this line, at about 5:50 of the video -

  const checked = checkbox.checked;

// how does checkbox.checked work? 
//I understand it's a true or false value, but why doesn't it have to have an 'if true' statement?

3 Answers

Steven Parker
Steven Parker
231,261 Points

You're right, it will require an "if" statement to test the value. By itself, the assignment doesn't cause any decision to be made by the code. Continue on in the video until about 6:50 and you will see this statement written:

if (checked) {

Isn't that exactly what you were expecting?

Steven Parker
Steven Parker
231,261 Points

The assignment line just puts a "true" or "false" value into the constant named "checked". This value represents the condition of the box at the moment the assignment runs. The value will be "true" if the box is checked, and "false" if it is not. But on this line, the value is only stored. Is isn't tested until later by the "if".

Does that answer your question?

Colin Sandlin
Colin Sandlin
4,512 Points

Steven, thanks for your reply. That is what I was expecting, however I still can't seem to understand what the checkbox.checked is doing or how that line operates prior to the 'if' statement?

I'm not sure if my question is making sense, sorry!

Colin Sandlin
Colin Sandlin
4,512 Points

Ah, yes it does. I think the syntax of the variable being named 'checked' and having that same terminology later in that line was messing with me. I appreciate your patience.