vendredi 23 août 2019

how to reduce if statements in javascript?

I'm writing code in vanilla JavaScript but I don't want to write a thousand different if statements.

I already tried searching up how to reduce if statements in JavaScript, but I didn't find anything helpful.

Here is some example code:

if (a == "text" && b == "othertext") {
  console.log("message");
} else if (a == "text2" && b == "othertext2") {
  console.log("other message");
} else if (a == "text3" && b == "othertext3") {
  console.log("other other message");
} else if (a == "text4" && b == "othertext4") {
  console.log("other other other message");
} else if (a == "text5" && b == "othertext5") {
  console.log("other other other other message");
} else if (a == "text6" && b == "othertext6") {
  // .. and so on.
}

If anyone can help me, it would be appreciated

Aucun commentaire:

Enregistrer un commentaire