#14 Switch Statement in JavaScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is davin reddy and let's continue with the series on javascript now in this video we'll talk about a switch case now what is switch here so let's that's an example here so i want to print at what time or maybe i want to set an alarm so based on a day example on monday i want to wake up at 7 00 am on tuesday i want to wake up at 4 00 am so as a trainer i have a contract with different companies and based on that i have to take a training for different countries and we have to get up at different times so when i'm taking training for the company in uk i have a different time zone right i have to wake up at different time so when i take a training for singapore badge i have to wake up at different time so the alarm changes per day right that's tricky i know but i want to create an example here where based on the day i want to set an alarm example on monday i want to wake up at let's say 7 am then on a tuesday because my back starts from tuesday to let's say thursday uh it depends actually but let's say till thursday i want to wake up at 4 00 am because my bad starts at 5 and and let's say on friday i want to wake up because right i don't have any work to i will get up at let's say 9 00 am and that extreme and on sunday and saturday sunday i have a youtube work so let's say i wake up at 8 00 a.m it's not a hardcore day so i get up at 8 anytime on weekends so this is my schedule right so i just want to wake up based on this okay uh so how do i do that of course we will not be building an app for alarm here which is a very big application here we just want to print at what time i should get up based on a day you pass example if i pass a day here and if i let's say the day is if i pass a monday it should print 7 am if i pass let's say friday it should print 9 a.m okay that's the only thing i want to do so what do you think what should be done here now one thing we know we can use an option of if else right so we can check for each condition so uh we'll check if the day is monday will simply print 7 am but do we have a better option from that in fact what i would suggest is pause this video complete this example with the help of e files comment your answer and then we'll move towards the alternate even if you don't know how to solve this scroll through the comments someone would have commented the right answer using e files refer that and then we'll see the solution so the solution is not to use if else of course that can be done but we will be using a special thing because any files you have to apply multiple conditions right because if you observe we have to compile each day then you have to print instead of that we can simply use a thing called switch now in switch again a new keyword in this switch you have to pass a variable in this case the variable for us is day so we are passing day and it will open this block now based on what day you have passed you have to print it right so it will check for multiple cases okay comparisons so we'll write the first case here the first case is let's say monday of course i'm interchanging double quotes single quote but that's that perfectly works if the day is monday in this case i will print 7am as simple as that okay and then what if the case is let's say tuesday in this case i will simply say tuesday and then we'll print i know it will take a lot of time to type this so let me just complete the entire stuff and let me just fast forward that okay so let's say this is the entire scale right so based on the day i have entered all the options here right so every friday it will print 9 a.m so i just want to check if this is working so how it works is whatever day you pass it will simply take that day in this variable and then it will it will try to match so whatever you have passed will match with the case okay uh will this work so what we'll do is starting with monday let's start with sunday because that's how we start our week right okay i know that's weird but let's do that so let me just print this quote and let's see if this works if i run this code that perfectly works you can see on sunday i'll be reading at 8 00 am that is working but how about if i make it monday now that's tricky uh it's because if you see the output it is showing all the alarms here seven am four am nine am eight am why is it because that's how switch case works the moment it tries to match in the other case it was trying to match with saturday right so when you say saturday it will check for the first case is it saturday of course not is it matching with tuesday no is it matching with wednesday no it will go ahead go ahead at the end when you say sunday oh it was sunday right so it will match with sunday and it will print 8 am now since none of the cases matches at the top it will go till the end it will print 8 a.m but in this case we are saying monday right so it is matching at the start itself once you match the case it will simply fall through it will print all the consoles it will it will simply say hey i got my match let me print everything which i can see on the screen or in the blog in this case you have to mention hey once you your case matching is done and once you feel hey you got monday that's great just get out of this case okay that's you have to mention just get out so after every thing you have to mention that we'll do that up to some time so what i will do here is i will simply print and you can see we got 7 am so what is this break so break means jump out of the block so you can see we have switch and when you say match once it is matching it will go to break and break will say hey get out get out of this block and directly you're jumping outside the switch case but yes if you print something here and we can say bye or we can say get lost just for fun and if i run this code you can see we call it 7 am get lost instead of this we should say bye so buy will get printed because it is outside the switch blog so just printing 7 a.m it will print by and you can see we got it but now let's move towards tuesday wednesday and thursday if you can observe the block inside each case is same right so when you have the same block what you can actually do is you can skip this part technically we should write break in each one as well but since we have the same thing we can just mention once and that is in thursday so what it will do is it will simply take for tuesday if it is matching it will fall through it will reach till thursday now in thursday we can put a break so that we it will not fall through for friday for friday also we can mention break because it is different and for certain center we have same so we can just remove this and we can just get this for in fact we don't have to mention break at the end because we know it's the end let's try we got 7 am let's try it for let's say wednesday and you can see we got 4 am right so tuesday wednesday and thursday it's 4 am so that's how we can use a switch blog now there is one more what if i entered a day which is wrong what if i say holiday now in this case you can say it is not matching with any of the day here so we can write a case which is a default case so default simply means if none of this matching then you can go for default and default you can say sleep or we can say 7am and want the disco videos i know that's weird who will watch the videos on holidays but if you want to improve yourself that's what we do and then we have to also put break on sunday because we have another case after sunday so let's run this corner you can see we got uh so when you say it is holiday it will print 7 a.m watch telescope videos cool so that's how we can use a switch case i hope you enjoyed in this video let me in the comment section and yeah that's it from this video bye
Info
Channel: Telusko
Views: 11,314
Rating: 4.9619045 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: SJPsOFfvEQA
Channel Id: undefined
Length: 7min 58sec (478 seconds)
Published: Wed May 26 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.