10. JavaScript Tutorial | Switch case

Share this post on:

hey what’s going on guys thud Maya for telesco learnings and in this video tutorial we are going to be taking a look at the switch case control statement in JavaScript so in the previous couple of videos we’ve been going through control statements the different types of control statements and in the previous video you also saw the if-else control statement so if you have missed that video you can check it out in this playlist so coming to switch case in Java Script so switch case is another controlled statement which is a conditional control statement just like if else control statement however the usage of switch case is a little bit different so switch case is generally preferred over the if-else  when there are n number of test cases needs to be tested which means that you have a lot of test cases a lot of conditions to be checked so for example let’s say you are going to be taking a number from one to hundred from the user so let’s say the user enters one and then in the output you have to print its textual form that is one okay similarly if he understood you have to print two and this should go on till hundred  okay so all this goes to  hundred so you can see we have hundred different test cases to test and you can use the if else if and else control

statements but then if you go ahead and do that the code will also increase a  lot and it will not be very clean and against which case is also much more efficient in terms of execution time compared to if-else when it comes to  larger test cases or larger quantities of test cases so this is why switch case is usually preferred in JavaScript when there are n number of test cases now this control statement that a switch case in Java Script actually performs or works a little bit different compared to switch case in C++ or Java so if you are coming from those programming languages switch case in Java Script works a little bit different and we will talk about that in a minute but let’s first actually start off with the programming and let’s see our first question so let me just quickly first read the question and then we will start off with the programming and we will see the syntax of how to switch case works so the question  is we have to find day off week by  accepting its number

for example if we enter a 1 then the output should be sunday as the first day of week if you enter 2 should be Monday and so on and so forth okay so three would be Tuesday four would be Venice day and so on so here you can see we have seven different conditions or test cases to be performed and we can use if-else and elsif conditions but then as I mentioned that is not the efficient way to go ahead with you can use which case in this case so let’s quickly first create a variable so I’m going to say where D and I’m just going to hard-code the value as 1 so for simplicity again we are not taking input from the user from the webpage because we still have to perform Dom manipulations and understand what our Dom manipulations will come to that when we move out in the series ok so this is how we start off with the syntax of such case so  you just write and switch and inside that you pass the variable D so since we want to check for what number we are going to be passing in this variable we have to pass that variable in this opening and closing round brackets of the switch case so this is the syntax and then opening and closing curly braces is for the body of the switch case now inside this we have to test for seven different cases starting from one to seven because we have seven days of week right so so here’s of the syntax booth we have to return the case and then you have to actually write down the value that the D variable is going to be holding so the value is going to be ranging from one to seven right so I’m going to say one and then give a pull in and below that I’m gonna write  what I want when the case is one okay so I’m going to say document not right in the edge to tag I’m going to say Sunday so if value of D is one you can see immediately we are getting an output of Sunday so inside this I’m also going to use a break statement

so remember break statement also helps us in control statements and managing the control statements so what this statement does is once we get our output once we get our case this big help us to come out of the switch keys okay so obviously we’re gonna have multiple cases right so we are going to have case two also so inside this I’m just going to copy this and paste it over here and I’m gonna say Monday all right so again I’m going to include one break over here so if I don’t actually include this break what happens is the moment the program finds the right keys so right now our D variable is storing the value of one right which means that case number one is going to be executed because we are checking if the value is 1 all right so this case is going to be true so if I remove this break over your if I just comment it out

 

you can see along with Sunday even Monday is getting printed so what is happening is once the switch Keys finds the right case if there is no break statement all the cases below that case are going to be executed also so that’s why we have to include this break statement and this break statement helps us to get outside or get out of the switch case immediately otherwise all the rest of the cases are also gonna be executed even though we are not needing them so I hope you get the idea why we’re using the break statement also now similarly I am just gonna write all the cases for all the different days of the week so we have seven cases so let me just show you all those cases ok so as you

can see on the screen I have written all the different cases so we have case one case two cases three till case seven because we have seven days of the week and depending upon what the variable value is if I say the sixth output is gonna be Friday if I see seven it is going to be Saturday and so on and so forth now along with this what if I enter some random value okay so let’s see I enter 78 now obviously we don’t have a day of the week which is corresponding to 78 right so in that case.

we can have one more case which is known as default and this basically means that if any of the keys do not go so the switch case is going to go from case number 1 to 2 2 to 3 to 4 to 5 and in the end and if it doesn’t get any of the keys right or true the default case is going to be executed so in that situation what I’m gonna see is a wrong input so as you can see we got wrong input and this default case is not

necessary but if we just want to give the user a prompt that he’s entered some wrong value you can always include this default case in the switch keys, okay so this was our program that finds the day of the week by accepting its number so  any number you put in you get the corresponding value if I say 1 I get Sunday if I say 0 we get wrong input so our program is working perfectly fine now I want to talk about switch-case a little bit in detail because in JavaScript switch case works a little bit different compared to in C++ or Java so in JavaScript switch case can accept numbers it can also accept string values and can also accept boolean values okay so let me just first cut this program don’t worry I will be sharing all the code of this program in  the video description you can also get some theory so make sure you check out the video description and coming back to this so this D can have a string you know so you can see Monday and then in

the switch case I can see kiss and double quotes against a Monday : and the case I can see document dot write forced working lis so you can see we got the output and it’s not in a larger form because I have to put it in h2 tag so there you go you can see so you can see that it is accepting strings now you can also have a boolean value so if I see through so here I can see case two and again we are getting the output now again one more thing that is different  for the switch case in Java Script is we can also pass in certain expressions or certain calculations in the switch case so in the switch case what I can do is let’s see if this is a string value let’s see I am saying none my so in the switch case what I can do is I can concatenate another string and I can see some file which is my surname so totally it will be done my space sub file right so in the switch case it is done my space sub pun so in the case what I can do is I can see I’m checking for my space sub fun and they were you got the output that’s my name and that’s how it goes so you can see that switch case in  Java Script has a little bit more

functionality wherein you can add  certain things in switch keys extra compared to other general-purpose programming languages and yes there are   a few more differences and few more additions but I’m not going to complicate it in this video you will see certain different variations of switch case in other videos as you might so I just wanted to talk about a few differences in switch-case that javascript has compared to other programming languages, okay so that’s it for this video guy I hope you got a good idea  about what is switch-case and when we used which case and how to switch case is different in JavaScript so thanks for watching see you guys in the next video peace

Share this post on:

Leave a Reply