Suppose that we have a gender variable, and the value of that gender is male, and it could change depending upon the user who is using our program. So it could be a male or a female. But we want are programmed to do different things, if it was a male, and other things if it was a female, and in order to let our program do so we use switch. And to use a switch, you just text switch, and you pass the value that can be changed. And in other program, the gender value can be changed. So here, I'm gonna pass gender and then you add curly braces, and inside it you would just add case, case and then and then courts and then the value that you want to check and I'm going to add male, and then colon.
And then what do you want to do in case the value equals male, and if it equals male, for example, I wanted to print out console dot log, I wanted to print out the word male to me. So I'm going to pass this gender or I can pass your male. And otherwise, if I want to check if it was a female I, I am going also to use case again, and add the value of female here in quotes inside quotes female. And I am going to add colon and then again, console dot log console dot log. And then I'm going to print the word female, the male and the good news is that switch statement gives us the ability to add a default values as templates. That gender was not specified at all, we can just add a default value by using the default.
And then we do anything here. initialize the gender variable or just print that the value has not been specified. I can say here, no value was given, was given. And a very important thing that you need to do before you run our program, you need to add a keyword called called the break. Because if you don't, or your program will just keep moving, it will go to the case mail and it's going to print mail and then it's going to go to female and do the same and print all these lines and we don't want that what we want to do. What we wanted to do is just check revalue and do what is inside that what is below that value.
And then it gets out of this switch statement. So you need to add the word break. And of course, you can add it, you should add it for all of these things here and here. Now if I save and run, as you can see I get mail, because the value the gender value now is currently equal to male if I change this to female, female, male and run our program again, I get female. But suppose that gender was not specified at all like this. If I save and run, I get no value was given.