Skip to content
Advertisement

Switch…case in JS

This simple problem gives me an error. Does not get the correct answer. I will be glad if you help.

JavaScript

Output: You did not pass

Advertisement

Answer

this way

JavaScript

can you explain why we write true? – Hussein Nadjafli (PO)

The JS switch only works on strict equality.

JavaScript

is equivalent to

JavaScript

in your code you replace the possible values [’x1’,’x2’,…] with an evaluation like

JavaScript

which returns either true or false
so your code becomes:

JavaScript

by replacing the A by true you therefore have a comparison between:

JavaScript

You can also do:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement