We can check if a character is uppercase in JavaScript by checking if the letter is equal to the letter after applying the JavaScript toUpperCase() method. Here is a simple function to do this:
function checkCharUpper(letter){
return letter == letter.toUpperCase();
};
Here is the function in use with an a couple of examples:
function checkCharUpper(letter){
return letter == letter.toUpperCase();
};
console.log(checkCharUpper("a"));
console.log(checkCharUpper("A"));
#Output:
False
True
When processing strings in a program, it can be useful to know which letters are uppercase and which characters are lowercase. Using JavaScript, we can easily check if a character is uppercase with the help of the JavaScript toUpperCase() method.
To check if a letter is uppercase, we just need to check if that letter is equal to that letter after applying the toUpperCase() method to it.
Below is our JavaScript function again which will check if a character is uppercase.
function checkCharUpper(letter){
return letter == letter.toUpperCase()
};
How to Check if a Letter is Lowercase in JavaScript
We can also check if a character is lowercase in JavaScript very easily.
To check if a letter is lowercase in JavaScript, we can adjust our function that we defined above to use the JavaScript toLowerCase() method instead of the toUpperCase() method.
Below is a JavaScript function that will check if a character is lowercase.
function checkCharLower(letter){
return letter == letter.toLowerCase();
};
And here is the function in use with an a couple of examples:
function checkCharLower(letter){
return letter == letter.toLowerCase();
};
console.log(checkCharLower("a"));
console.log(checkCharLower("A"));
#Output:
True
False
Hopefully this article has been useful for you to learn how to check if a character is uppercase in JavaScript.
Leave a Reply