Replacing If Else Chains with Switch
function chainToSwitch(val) {
var answer = "";
if (val === "bob") {
answer = "Marley";
} else if (val === 42) {
answer = "The Answer";
} else if (val === 1) {
answer = "There is no #1";
}
// and now with switch
switch(val){
case "bob":
answer = "Marley";
break;
case 42:
answer = "The Answer";
break;
case 1:
answer = "There is no #1";
break;
}
return answer;
}function chainToSwitch(val) {
var answer = "";
switch(val){
case "bob":
answer = "Marley";
break;
case 42:
answer = "The Answer";
break;
case 1:
answer = "There is no #1";
break;
case 99:
answer = "Missed me by this much!";
break;
case "John":
answer = "";
break;
case 156:
answer = "";
break;
default:
answer = "Ate Nine";
}
return answer;
}
chainToSwitch(7);
Also in JavaScript:
- js jquery class ending with string
- react native scrollview
- detect if user is online react
- prevent form submission using jquery
- how to add elements in array in javascript
- javascript remove from array by index
- how to remove an object from array in react native
- remove a value to an array of javascript
- how to go back one directory in git bash
- get value onChange from mat-select angular
- jquery click event
- javascript Count the frequency of a value in an array
- target data option select vue js
- summernote mentions ajax
- get random entry from array javascript
- command reboot android app react native adb command
- javascript create checkbox with label
- javascript input onchange get value
- javascript get same elments from multiple arrays
- jason rpc reactjs
- how to redirect to another page in javascript on submit type
- how to add make touchstart passive in jquery "3.4.0"
- jquery selector checked
- returned data has p tags react