Learn Switch Statements In 7 Minutes

preview_player
Показать описание

The switch statement is a really underutilized syntax that can drastically clean up specific long if else statements by making them easier to read and use.

📚 Materials/References:

🌎 Find Me Here:

⏱️ Timestamps:

00:00 - Introduction
00:30 - Code Demo
01:08 - Switch Statement Basics
04:45 - Matching Multiple Cases
05:59 - Common Mistakes

#SwitchStatement #WDS #JavaScript
Рекомендации по теме
Комментарии
Автор

thanks! this helped me realize a switch block was NOT the correct way to handle what i was doing 😂

elokthewizard
Автор

your teaching style is so great, thank you very much!

rpf
Автор

tbh using Objects/Maps instead of switch usually is much more readable and convenient.
You can use ?? to implement defaults, some prototype key collision(like valueOf) is not an issue 99% of the time(and can be avoided with null prototype or Map), having multiple values match the same case can also be fixed with Map.
You can't really implement a case without a break tho unless you get really verbose but then just actually use a switch - but honestly how often do you even need this in a switch statement? If anything it makes your code even less readable when you have to consider that some cases don't cause a break.

using switch over if else doesn't bring any benefits really - I find them both to be quite unreadable and verbose and I think performance-wise they're literally the same.

huge_letters
Автор

Kyle, what is your opinion on use of switch statements versus objects with keys being the case constants and values being the case blocks wrapped in functions? Of course readability will take a hit, but will that not make the code more declarative and hence reactive?

pranavbhat
Автор

Your videos are so easy to understand thanks Bro👌👌

AmaniTinbite
Автор

In php I sometimes do this:

switch(true) {
case ($myVar1 < 5): {echo "case 2 " ;break;}
case ($myVar2 != "foo" ): {echo "bar" ;break;}
case ($myVar3+2 < 5): {echo "foo bar" ;break;}
}

torbenwranglaursen
Автор

Never tired watching and executing your tutorial ❤

BryanGranseDevs
Автор

Thank you so much, you explain magnificantly

sixirabrawl
Автор

Your videos are the best in YouTube. But this one was simple and trivial.
By the way, Thank you

erfanvatanparasti
Автор

But you can use switch statements to check if, say, a number is less than 5. You just need to gently abuse what a switch is used for. The expression in a switch statement is the value you are testing your cases against, and if the derived value of the case matches the derived value of the expression then a Boolean true happens which runs the code inside the matching case.

So, you can shortcut a switch by setting the expression to be a Boolean true and then do whatever you want in the cases

const x = 50;

switch (true) {
case x < 50:
console.log("Less than 50");
break;

case x > 50:
console.log("Greater than 50");
break;

case x === 50:
console.log("Exactly 50");
break;

default:
console.log("x isn't a Number");
}

rossclutterbuck
Автор

I generally don't like switch statements, they take a lot of space and look ugly af (thats personal preference tho), also the fact it does the fallover when you dont have break is an easy way to end up with a bug that's not very obvious (thats kinda depending on what type of logic is in there), what I tend to do instead is just use maps/objects (or whatever u call the equivalent in any given language) and just map the values straight to the logic that handles them

funkenjoyer
Автор

Can you repeat this example using object literals instead?

LucasNovaes
Автор

you can also drop the "break" if you add "return"

noriller
Автор

I much rather a lookup table/dictionary but the problem is most people use object literal when they should be using map

firedforfighting
Автор

Hey bro, how are you changing multiple words at once for example you highlighted cat and changed it to bobcat twice

Smurfis
Автор

Kyle is super awesome at teaching stuff. That being said, I just don't see the need to get into Switch Statements unless there is a huge performance increase. It can't do anything else other than equals, needs breaks... whereas if/else is much easier for a newb like myself to understand and all Comparison Operators work with it. Can anyone please share the more advantages to Switch?

consigiere
Автор

Hey bro I had developed a simple react website it's running properly in firefox but lagging in chrome and other browser

knull
Автор

you can put the default case first btw

soniablanche
Автор

Switch case is antipattern. Better way is to use hash (object)

nikita_suiazov
Автор

how bout using object literal for simple cases like this one.

metalsadman