So JavaScript has different parts to learn, switch is something in conditional part , okay let's make some question's before we learn
1) what are Conditions ?
2) why its So important in Coding?
3) What are the types of Conditional Statements?
4) Why Switch?
5) When to use Switch ?
So conditions are Basically an code which allow us to make
something to 'happen or execute' when Something is going to be true or false ,
consider a scenario of getting a new Internship , the company is going to have 'Set of Rules' to follow on selecting an candidate
like ...
1) candidate got a degree
2) Candidate got experience in the domain
3) blah...blah...
and finally from god grace(if he is there ??) we will get an internship if all the conditions are true.
Conditions are Nothing but 'Set of Rules'
While Programing we need to tell the computer to check Something
on Executing , We can say it in multiple ways to the Computer this is where the Conditional Statements come in Role, the frequently used are :
1) if..else..
2) if..elseif..else..
3) Switch
So if your opening Google and Searching for Flowers Colour, and
Google has to respond with based on the Search keyword , as developer how will you program ,
we Can use an if && else if , lets see how it works
let us assume that we will have 3 major types of searches initially Rose, Sunflower, Violet.
lets code using if else if...
```
if(flower == 'rose'){
console.log("Roses are red");
}else if(flower == 'violet'){
console.log("Violets are blue");
}else if(flower == 'Sunflower'){
console.log("Sunflower are yellow");
}else {
console.log("Please select the flower")
}
```
So from the above Code you can see that there is Weird Structure of Code that goes in some degree , and if we continue this can make an unclean Code, So lets see what this will look with an Switch Conditional Statement....
var flower = "tulip";
switch (flower){
case "rose":
console.log("Roses are red");
break;
case "violet":
console.log("Violets are blue");
break;
case "sunflower":
console.log("Sunflowers are yellow");
break;
default:
console.log("Please select another flower");
}
Wasn't easy As a Developer you Would Always Have a doubt when to use Switch Statement , So as per my View We can use Switch Statement While we have an Specific Conditional Cases.
This is it …Comment your opinions and Correct me if am Wrong..
Top comments (0)