# Switch-case vs. if/else (v13)

## Setup

``````var v = 0 | Math.random() * 9 + 1; // 1 to 9
var sum;
const sumObj = {
1: "A",
2: "B",
3: "C",
4: "D",
5: "E",
6: "F",
7: "G",
8: "H",
9: "I"
};

const sumArr = ['A','B','C','D','E','F','I','G','H','I']``````

## Test runner

Testing in
TestOps/sec
if/else strict
``````if (v === 1) {
sum = "A";
} else if (v === 2) {
sum = "B";
} else if (v === 3) {
sum = "C";
} else if (v === 4) {
sum = "D";
} else if (v === 5) {
sum = "E";
} else if (v === 6) {
sum = "F";
} else if (v === 7) {
sum = "G";
} else if (v === 8) {
sum = "H";
} else if (v === 9) {
sum = "I";
} else {
sum = "?";
}``````
Array
``sum = sumObj[v -1] ||  "?";``
object
``sum = sumObj[v] ||  "?";``
Switch-case
``````switch (v) {
case 1:
sum = "A";
break;
case 2:
sum = "B";
break;
case 3:
sum = "C";
break;
case 4:
sum = "D";
break;
case 5:
sum = "E";
break;
case 6:
sum = "F";
break;
case 7:
sum = "G";
break;
case 8:
sum = "H";
break;
case 9:
sum = "I";
break;
default:
sum = "?";
break;
}``````
if/else
``````if (v == 1) {
sum = "A";
} else if (v == 2) {
sum = "B";
} else if (v == 3) {
sum = "C";
} else if (v == 4) {
sum = "D";
} else if (v == 5) {
sum = "E";
} else if (v == 6) {
sum = "F";
} else if (v == 7) {
sum = "G";
} else if (v == 8) {
sum = "H";
} else if (v == 9) {
sum = "I";
} else {
sum = "?";
}``````

## Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.