How to Check Case of Letter in JavaScript ?
Last Updated :
05 Jul, 2024
There are different ways to check the case of the letters in JavaScript which are explained below comprehensively, one can choose the method that best suits their specific use case and requirement.
Using the function "toUpperCase()" or "toLowerCase"
Javascript has toUpperCase() and toLowerCase() methods by using these methods one can convert the string to an upper case or lower case letter and then use it to compare it with the original letter to determine the case of the letter.
Syntax:
character.toUpperCase();
Example: Demonstrating the use of the "toUpperCase()" and "toLowerCase()" methods to determine the case of the letters in JavaScript.
JavaScript
function checkCase(character) {
if (character ===
character.toUpperCase()) {
return 'Uppercase';
} else if (character ===
character.toLowerCase()) {
return 'Lowercase';
} else {
return 'Mixed case';
}
}
console.log(checkCase("G"));
console.log(checkCase("g"));
console.log(checkCase("GeeksforGeeks"));
OutputUppercase
Lowercase
Mixed case
Using regular expression
One can use regular expressions to check whether the letters are in upper case or lower case.
Syntax:
/[A-Z]/ // Regex for only uppercase letters
Example: Demonstrating the use of the regular expression to check the case of the letter.
JavaScript
function checkCase(character) {
return (/[A-Z]/.test(character)) ? 'Uppercase' : 'Lowercase';
}
console.log(checkCase("G"))
console.log(checkCase("g"))
OutputUppercase
Lowercase
Using ASCII value
ASCII values can also be used to check the case of the letter as the ASCII values between 65 and 90 are in upper case while the ASCII values from 97 to 122 are in lower case.
Example: Demonstrating the use of ASCII to determine the case of the letter.
JavaScript
function checkCase(str) {
if (str.charCodeAt(0) >= 65 &&
str.charCodeAt(0) <= 90) {
console.log("Letter is UppperCase")
}
else if (str.charCodeAt(0) >= 97 &&
str.charCodeAt(0) <= 122) {
console.log("Letter is in lowerCase")
}
}
checkCase("G")
checkCase("g")
OutputLetter is UppperCase
Letter is in lowerCase
Using localeCompare Method
The localeCompare method can be used to compare a character with its uppercase or lowercase version. Depending on the result, you can determine the case of the letter
Example: Demonstrating the use of the localeCompare method to determine the case of the letters in JavaScript.
JavaScript
function checkCase(character) {
if (character.length !== 1) {
return 'Mixed case';
}
if (character.localeCompare(character.toUpperCase()) === 0) {
return 'Uppercase';
} else if (character.localeCompare(character.toLowerCase()) === 0) {
return 'Lowercase';
} else {
return 'Mixed case';
}
}
console.log(checkCase("G"));
console.log(checkCase("g"));
console.log(checkCase("GeeksforGeeks"));
OutputUppercase
Lowercase
Mixed case
Using Character Comparison
In this approach we will check if character is between 'A' and 'Z' then it is Uppercase and if it's between 'a' and 'z' it is Lowercase.
Example: This example uses character Comparison to check Case of Letter.
JavaScript
let letter = 'g';
// Check if uppercase
if (letter >= 'A' && letter <= 'Z') {
console.log("Uppercase");
}
// Check if lowercase
if (letter >= 'a' && letter <= 'z') {
console.log("Lowercase");
}
Similar Reads
How to Convert Camel Case String to Snake Case in JavaScript ? We are going to learn about the conversion of camel case string into snake case by using JavaScript. Camel case string means words combined, each starting with uppercase and Snake case means words joined with underscores, all lowercase, Converting camel case to snake case involves transforming strin
3 min read
JavaScript Program to Check if a Character is Vowel or Not In this article, we will check if the input character is a vowel or not using JavaScript. Vowel characters are âaâ, âeâ, âiâ, âoâ, and âuâ. All other characters are not vowels. Examples: Input : 'u'Output : TrueExplanation: 'u' is among the vowel characters family (a,e,i,o,u).Input : 'b'Output : Fal
4 min read
JavaScript Program to Check if Two Strings are Same or Not In this article, we are going to implement a JavaScript program to check whether two strings are the same or not. If they are the same then we will return true else we will return false.Examples: Input: str1 = Geeks, str2 = GeeksOutput: True. Strings are the SameInput: str1 = Geeks, str2 = GeekOutpu
4 min read
JavaScript Program to Print the First Letter of Each Word Printing the first letter of each word involves extracting the initial character from every word in a given string, typically accomplished by splitting the string into words and selecting the first character from each resulting word. Examples of Printing the First Letter of Each Word Table of Conten
3 min read
JavaScript Program to Count Number of Alphabets We are going to implement an algorithm by which we can count the number of alphabets present in a given string. A string can consist of numbers, special characters, or alphabets.Examples:Input: Str = "adjfjh23"Output: 6Explanation: Only the last 2 are not alphabets.Input: Str = "n0ji#k$"Output: 4Exp
3 min read