character === character.toUpperCase()
will be true
if character
is uppercase.In this example isUppercase
is true
:
const character = "A";
const isUppercase = character === character.toUpperCase();
In this one it is false
:
const character = "a";
const isUppercase = character === character.toUpperCase();
Same code can be used to check if the whole string is uppercase:
const character = "HELLO";
const isUppercase = character === character.toUpperCase();
Finally, you can check if a single character of a longer string is uppercase with charAt
:
const string = "Hello World!";
// true
if (string.charAt(0) === string.charAt(0).toUpperCase()) {
console.log("This will be logged");
}
// false
if (string.charAt(1) === string.charAt(1).toUpperCase()) {
console.log("This won't be logged");
}