FAQ: Conditional Statements - The switch keyword

Not sure what switch has to do with array indices. Sorry, I’m not sure what you are asking.

Acho que nao to sabendo fazer a pergunta corretamente; pq li que metodos sao acoes que podem ser executadas com os objetos (nesse caso da string) como que isso acontece? essa diferenciação de maiúscula e minúscula.

I’m not sure I really understand your fixation with these two very trivial methods of the string object. One converts all letters to upper case, the other all letters to lowercase. In either event we are certain what the outcome is and can proceed from there to make comparisons.

ahn sim, nada de mais so fiquei pensando como que esse metodo olha para strings e as modifica.segue; obg. :slight_smile:

The methods act upon a string object in their context.

a = 'abcdefg'.toUpperCase()
console.log(a)    //  ABCDEFG

Methods are like functions but many don’t take an argument. The context is the argument, as such. In this case the object is a string which has inherited the method from its parent class, String. This will be covered in the unit on Classes so don’t be too hung up with it and stay focused on the topic at hand, switch().

3 posts were merged into an existing topic: Switch keyword syntax error

I’m wondering is the advantage of the switch statement to do with the ‘break’? Is it more efficient to compute than else if because the ‘break’ allows it to be run part way and stop at a ‘break’?

“break” just allows it to exit the sequence since the conditional is found to be true. No need to run the rest of the statement. To answer your question, yes this is more efficient and theoretically uses less code. I find using the ternary operator easier and more efficient than using switch statement. hope that helps.

1 Like

Yes it does, Thanks, Jeffa

1 Like

Is there a reason that the Default statement must go last or that the exercise says it has to be last? It seems to work when making the default statement first before any case keywords. I even tried throwing it randomly in between 2 cases and it still worked. Is it just easier to read when it’s last rather than first?

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!’);

default:
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

OR

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…
switch
case
console.log
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
default:
  // 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