[ change ]. Add this disclaimer: this switch use is considered EVIL or ABUSIVE. Some Javascript students also see the use of tees as their primary SIN. Therefore, be careful with their use, because hell can wait for you, so the gospel is coming.
You can use switch like this 1 :
switch (true) { case (parseInt(charCode) >= 65 && parseInt(charCode) <=90): //UPPERCASE alert("UP"); break; case (parseInt(charCode) >= 97 && parseInt(charCode) <=122): //LOWERCASE alert("LO"); break; case (parseInt(charCode) >= 48 && parseInt(charCode) <=57): //NNUMBERS alert("NUM"); break; default: break }
it is rather bulky. You do not need to use parseInt if you got a charCode coming from event.keyCode . If you need to use parseInt , be sure to provide radix or, better yet, use Number to convert to a number.
In any case, using ternary is an alternative for this:
alert( charCode >= 65 && charCode <= 90 ? 'UP' : charCode >= 97 && charCode <= 122 ? 'LO' : charCode >= 48 && charCode <= 57 ? 'NUM' : 'OTHER' );
[ change ]. See if the following alternative can satisfy Javascript Coders Church ...
Another alternative is to use RegExp with a character derived from keyCode :
var chr = String.fromCharCode(charCode), alertval = /[az]/.test(chr) ? 'LO' : /[AZ]/.test(chr) ? 'UP' : /[0-9]/.test(chr) ? 'NUM' : 'OTHER'; alert(alertval);
As a final option (man, javascript is so universal!) I present:
var check = String.fromCharCode(charCode) .replace(/([az])|([AZ])|([0-9])|(.+)/, function(a,b,c,d) { return b ? 'LO' : c ? 'UP' : d ? 'NUM' : 'OTH'; });
1 some notes about this "trick"