JavaScript Switch Statements - When to use switch over if/else?

preview_player
Показать описание
Switch statements are a great way to direct application logic flow with conditional expressions. The tricky part with them though is that it's not entirely clear when developers should use switch statements. In this video, I'm sharing my opinions on when developers should use switch through my experiences with the JavaScript feature

Timestamp:
0:00 Introduction
0:33 Switch Statement Syntax
1:42 When should I use Switch Statements?
2:03 Example: Switch Statements used for Handling State
3:12 Convert Example: Switch Statement into if/else statement
4:13 Redux State using Switch Statements
4:49 switch Feature: return and break keywords
6:03 switch Feature: Multicases for Switch Statements
6:41 Switch Statements: Big Takeaway + Advice
7:28 Volunteer to the Igbo API

== Let's Connect ==

== Who I Am ==
I'm Ijemma, a frontend software engineer currently working in New York City. I make videos on all things JavaScript and, more broadly, frontend web development.

Icons from Freepik

#javascript #webdevelopment #softwareengineer
Рекомендации по теме
Комментарии
Автор

So many good things about your videos. From the top-notch production quality, to beginner friendly and easily digestible code snippets. I enjoy watching your videos.

tobidaada
Автор

I appreciate the video! I have asked the same question to developers I know and the answer wasn't always clear or if there was a reason why I should chose one over the other. A lot will have to do to preferences of a lead or the company you work for, but now I can see why it might be valuable instead of dismissing it completely.

Sturdy_Penguin
Автор

finally a clear explanation in understandable terms! Also the Igbo API project sounds wonderful and needed in a time when the culture is spreading needs to become international

goldlenz
Автор

Extremely good quality. The quality of sound was amazing and the way you explained is really great. Keep it up

manto
Автор

the examples are so satisfying especially with the comparison to the if statement block!!

CarmelleCodes
Автор

week 8 of javascript... this was very helpful, Thanks kid!

labombarde
Автор

This is great. Showing this really just lets us see how switch is so much cleaner.

emanuelzhimrules
Автор

Thanks for the video I am trying to implement switch case on checking if cookies are set so that a pop up form isn't actioned. I realised that I have too many if else if statements

rufaro
Автор

Woww you did a phenomenal job explaining when to use switch statement. I'm going to start implementing that In my code.

carldjovan
Автор

This is huuuge help! Greetings from Egypt!

mohamedaboelfotoh
Автор

i dream a world in which i can use break keyword in if/else

Lemmy
Автор

whats the difference between a "switch()" and "switch ()" with a space?

i dont know what to use

r
Автор

Imo Switch statements are kinda overrated. They make the condition check a lot shorter but bloat the code with ‘break’ statements. They make things easier as you’ve pointed out, but I would totally be down for JS supporting a new ‘arrow syntax’ and making them expressions instead of statements like Java 14 did. This way identical cases are comma separated, ‘break’s are retired, and you don’t have to define an uninitialized result variable

UsernameUsername
Автор

I think switch is very ancient solution that is not flexible at all. For example:

const newCase1 = () => console.log("newCase1");
const newCase2 = () => console.log("newCase2");

const newSwitch = {
newCase1,
newCase2
}

newSwitch.newCase1();

const ourCase = "newCase2";
newSwitch[ourCase]();

// Hey we have brand new dynamic case!
newSwitch.newCase3 = () => console.log("newCase3");
newSwitch.newCase3();

vitiok
Автор

Or you could just type:

console.log(`The person is ${person.state}`);


🤓

caribbeanman