FAQ: Conditional Statements - The switch keyword

Welcome to the forums!

It’s convention that default is the last case, but it’s not something that’s mandatory. It’s just much more common to have it last, and can make code more readable by others (and also prevent any errors in case you forget to put a break statement under the case).

1 Like

Ok i have to say this specific challange is not well worded at all, it leaves a ton to be interpreted without any indications. I have no idea what the first problem is asking for even after seeing the answer, overall not great.

What does the green mean in code? for ex. the ‘default’ or the ‘log’ in console.log().

console - Web APIs | MDN

console is an object, and objects have properties (bound values) and methods (bound functions). .log() is a method of the console object for logging out (streaming) textual data to the console.

I don’t see, .default() in the methods list, but that might mean just more digging.

I’m a new learner here too. Seems to me that it is easier to read and lesser syntax to input when you use switch

Seems to me that the Switch syntax is not perfectly designed for its intended use yet? My first instinct will be to leave out
‘break’ just like how the ‘if’ statements work. Or is there other intended use of the Switch syntax such that we have to insert either return or break?

switch (athleteFinalPosition) {
case ‘first place’:
return console.log(‘You get the gold medal!’);

case ‘second place’:
return console.log(‘You get the silver medal!’);

case ‘third place’:
return console.log(‘You get the bronze medal!’);

return console.log(‘No medal awarded.’);

The switch is kind of syntactic sugar in simple cases, but it promises a selection environment where there can be mass amounts of dynamically generated cases. Wherever one can, an object is often the way to go.

When we return a console.log() we’re sending undefined back to the caller. One is better served to return the value, and log it at the caller.

There is nothing wrong with your code. It warrants looking for and mitigating repetition. That, and having functions return a value that is not undefined. Let the action happen as a result of the return, rather than inside the function.

Pardon me for not answering the other part of your question about, break.

Whenever return is used in the case statement, break would be unreachable when written after the return. One or the other; never both.

Pseudo code:

switch expression
case expression : return value
default: return default value


switch expression
case expression : do action; break;
default: do default action
1 Like

my question is about readability and not so much about this lesson in my upload you will see a difference in the whitespace 's at the start of each line of code. I’m not sure how to word this as i don’t know if there is term for how it steps out further with the whitespace…
Now my actual question is how to get it to or most likely " what am i doing wrong" that my code doesn’t look like the example.
Thank you for your time.

Given that JS ignores most white space, how we present the source code is purely aesthetic. There is absolutely nothing wrong with how your code is presented. The main thing is we can see the cases and their corresponding actions, and it is easy to see where the switch ends.

Won’t say I have a hard and fast rule, but, one generally does not need to indent the case clause, but it looks even more definitive if the action is indented.

switch (..) {
case ..:
  // action
case ..:
  // action
  // action

thank you the word ‘indent’ was driving me nuts trying to figure it out. I noticed that it auto indent’s sometimes and does not others. Is it up to the coder and or how he/she writes the code after the initial " { +enter " to use additional spaces and back spaces to further indent as they see fit.

1 Like

I’m late, but this may help someone else.
It will execute all cases AFTER the first match.
In this example of first, second and third place, it executes all of them when you delete break because it matches with the first case.

Does switch statements when evaluating standalone expressions, use == (type coercion) or === (type checking)?
switch (myVar) {
case ‘myVar’: // ← myVar == ‘myVar’ or myVar === ‘myVar’
Thanks in advance.

A simple test will tell us…

switch (true) {
    case 1: console.log('coercion'); break;
    default: console.log('no coercion')
<- no coercion

If there was coercion the first case would be the followed branch.

1 Like
let athleteFinalPosition = 'first place';

switch (athleteFinalPosition) {
  case 'first place':
    console.log('You get the gold medal!');
  case 'second place':
    console.log('You get the silver medal!');
  case 'third place':
    console.log('You get the bronze medal!');
    console.log('No medal awarded.')

For some reason the default statement is not working here. If I change var to ‘firstt place’ nothing happens and the console keeps asking me if I added a default statement to my code. So even when the everything is as it says in the instructions nothing happens and i keep getting asked did i add a default statement. How would i fix this?

You misspelled default.

1 Like

WOW, I can’t believe i didn’t see that this whole time. I spelled it right in the comment but not in my program. Thank you.

let athleteFinalPosition = 1;
switch (athleteFinalPosition) {
case (‘first place’ || 1) :
console.log(‘You get the gold medal!’);

It logs “none” to the console.
Why does the “||” OR function not work in this case?

The case expression must match the switch argument. Is `athleteFinalPosition’ a boolean?

Pardon my misconceptions, but what I’m thinking is that the switch argument here would be 1, and the case would be either a string or number.

Shouldn’t it fulfill the ‘OR’ condition when the case is checked? I did a bit of fiddling and it seems if the order is swapped to case( 1 || ‘first place’) , it prints the gold medal when athleteFinalPosition = 1

The equivalent “if” form would be:

let athleteFinalPosition = 1;
if (athleteFinalPosition === ‘first place’ || 1) {
console.log(‘You get the gold medal!’);
} else {

The only time we can use OR in the case expression is when the argument expression is also a boolean, such as true.