Home > Back-end >  how to find if a string includes something in a switch() javascript
how to find if a string includes something in a switch() javascript

Time:05-05

i would like to find out if a string contains a certain word using a switch() statement

here is an example of what i want to use that for:

let text = "among"

switch(text.toLowerCase().includes()){
   case "among"
      console.log("not funny")
      break

   default:
      break
}

CodePudding user response:

Switch statement evaluates an expression so this code will not work. You could store possible inclusions in an array and loop through the array, then depending on the evaluation output a certain response with a switch statement.

let text = "among"

let words = ["among", "text", "test"]


function test(word, wordArr) {
    for (let w of wordArr) {
        if (w === word) {
            switch (w) {
                case "among":
                    console.log("among word");
                    break;
                default:
                    break;
            }
        }
    }
}

console.log(test(text, words))
  • Related