In this very short article, we will learn how to check if the string is uppercase.
Let’s start with the very obvious and easy method by creating a function for it.
function checkUppercase (string) {
}
And we will just compare the string with string .upperCase (will take a string and make it uppercase) and return the boolean
function checkUppercase (string) {
if (string === string.toUpperCase()) {
return true
} else {
return false
}
}
or a slightly shorter version:
function checkUppercase(string) {
return string === string.toUpperCase()
}
and for some extra safety because there are apparently cases where punctuation, and numbers are a problem but personally snippet above worked without a problem. But if you want one extra check here it is:
function checkUppercase(string) {
return string== string.toUpperCase() && string!= string.toLowerCase()
}
And that’s how you check if the string is uppercase in javascript.