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

Why we have to use the double equal sign in the if condition?

Hi everyone,

My code only runs when the equal size in the if condition () is doubled, and the equal size in the {} is single. Could you please explain?

Here's my code:

toggleList.addEventListener ( 'click', () => { if (divList.style.display == "none") { divList.style.display = "block"; toggleList.textContent = "Hide List"; } else { divList.style.display = "none"; toggleList.textContent = "Show List"; }

});

Thank you so much!

2 Answers

== is an equality comparison operator, it is used to compare two values. So in your if statement you are asking JavaScript to see if divList.style.display is equal to "None".

= is an assignment operator, it tells JavaScript that you want to assign a value to something. So inside of your if/else code blocks you are assigning values to divList.style.display and toggleList.textContent.

So the short answer is that they do different things, and as such aren't interchangeable.

Thank you so much Andren!