เพื่อทดสอบว่าตัวอักษรในสตริงเป็นตัวพิมพ์ใหญ่หรือเล็กใน JavaScript คุณสามารถแปลงตัวอักษรนั้นเป็นตัวพิมพ์ใหญ่หรือเล็กแล้วดูผลลัพธ์
ตัวอย่าง
function checkCase(ch) { if (!isNaN(ch * 1)){ return 'ch is numeric'; } else { if (ch == ch.toUpperCase()) { return 'upper case'; } if (ch == ch.toLowerCase()){ return 'lower case'; } } } console.log(checkCase('a')) console.log(checkCase('A')) console.log(checkCase('1'))ผลลัพธ์
จะได้ผลลัพธ์ดังนี้
lower case upper case ch is numeric