Search code examples
javascriptwebcomparecase-sensitive

Compare strings without being case-sensitive


I have problem with a variable I made (it's a string) in JavaScript. It will be prompt from the user and then with the switch I will check if it is true or not. Then when I input it upper case it will say it is identified as a another var.

Here is my code:

var grade = prompt("Please enter your class") ;

switch ( grade ){
    
    case "firstclass" :
         alert("It's 500 $")
         break;
    case "economic" :
         alert("It's 250 $")
         break;
    default:
         alert("Sorry we dont have it right now");

}


Solution

  • as @nicael stated just lowercase what they input. However, if you need to preserve the way it was input and only compare using the lowercase equivalent, use this:

    var grade = prompt("Please enter your class") ;
    
    switch ( grade.toLowerCase() ){
    
      case "firstclass" :
         alert("It's $500");
         break;
      case "economic" :
         alert("It's $250");
         break;
      default :
         alert("Sorry we don't have it right now");
    }