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

Can someone explain to me what switch statement with case = "true" and case="false" means.

```<!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <div style="height:30px;width:30px;border:2px solid red;" role="checkbox" aria-checked="false" tabindex="0">

</div> <script> const div = document.querySelector('div') div.addEventListener('click',function(){ switch(div.getAttribute('aria-checked')){ case 'true': //Can you explain to me what this means case = 'true' div.setAttribute('aria-checked','false') break; case 'false': //Can you explain to me what this means case = 'false' div.setAttribute('aria-checked','true') break; } }) </script> </body> </html>

1 Answer

Reformatted code:

div.addEventListener('click', function () { 
  switch (div.getAttribute('aria-checked')) {
     case 'true': 
        div.setAttribute('aria-checked','false')
         break; 
     case 'false': 
         div.setAttribute('aria-checked','true') 
       break;
   }
});

This switch statement checks the value of div.getAttribute('aria-checked'), if the value is true, it will execute the code with the case 'true', div.setAttribute('aria-checked','false'). Otherwise it will execute the code within the case 'false' div.setAttribute('aria-checked','true') . Essentially alternating the attribute between true and false.