Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
218,832 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
218,832 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.