How can I check if a string is all uppercase in JavaScript? [duplicate]

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

Leave a Comment

tech