Magic 8 Ball - Javascript [=, ==, or === in a switch statement]

Hello, I’m currently working on:

I created a switch statement to cycle through the various responses, however I’m a little confused with the syntax. Say I have this switch case:

let eightBall = ‘’;
let randomNumber = math.floor(math.random() * 8);

switch (randomNumber) {
case randomNumber = 0:
eightBall = ‘It is certain’;

If I am checking for equivalence, shouldn’t randomNumber = 0 be randomNumber === 0 instead?

Hi, there!

Welcome to the forums!

This is not proper syntax.

The beginning of the switch statement checks the value of ‘randomNumber’ and compares it to each case. So, case only needs to be the value being compared to randomNumber


case 1:

Feel free to take a look at the Codecademy Docs on switch statements for more information! :slight_smile:


Hey @digital2666471401 ,

As @kirativewd said your syntax is a little off. Hopefully that makes sense! Your question brings up another point though that is interesting. Which is, how does the switch statement perform it’s comparison?

It looks like it does a strict comparison. So it automatically performs === for you. This means the values passed into case not only have to equal the value of randomNumber but also have the same type. So for example let’s say, for whatever reason, your randomNumber was returning numbers as strings, but you are putting numbers in your case statement. This would be a problem at compilation time.


1 Like