How can I check if a string is all uppercase in JavaScript?
I must write at least one sentence here because they don't like short answers here, but this is the simplest solution I can think of:
s.toUpperCase() === s
function isUpperCase(str) {
return str === str.toUpperCase();
}
isUpperCase("hello"); // false
isUpperCase("Hello"); // false
isUpperCase("HELLO"); // true
You could also augment String.prototype
:
String.prototype.isUpperCase = function() {
return this.valueOf().toUpperCase() === this.valueOf();
};
"Hello".isUpperCase(); // false
"HELLO".isUpperCase(); // true