Check if a String Contains Only Uppercase Letters

medium

By - Aman Pareek

Last Updated - 28/08/2024

Problem Statement

Write a function that determines if a given string contains only uppercase letters. The function should return true if the string is entirely uppercase and false otherwise.

Example 1

Input: str = "AMANPAREEK"

Output: true

Example 2

Input: str = "Aman"

Output: false

Solution 1: Using Regular Expression

function isAllUppercaseUsingRegex(str) {
  return /^[A-Z]+$/.test(str);
} 

const str1 = "AMANPAREEK";
isAllUppercaseUsingRegex(str1);  //output: true 

const str2 = "Aman";
isAllUppercaseUsingRegex(str2);  //output: false 

Solution 2: Using toUpperCase Method

function isAllUppercaseUsingToUpperCase(str) {
  return str === str.toUpperCase();
} 

const str1 = "AMANPAREEK";
isAllUppercaseUsingToUpperCase(str1);  //output: true 

const str2 = "Aman";
isAllUppercaseUsingToUpperCase(str2);  //output: false 

Solution 3: Using Array.prototype.every

function isAllUppercaseUsingEvery(str) {
  return [...str].every(char => char >= 'A' && char <= 'Z');
} 

const str1 = "AMANPAREEK";
isAllUppercaseUsingEvery(str1);  //output: true 

const str2 = "Aman";
isAllUppercaseUsingEvery(str2);  //output: false 

Solution 4: Using a for Loop

function isAllUppercaseUsingForLoop(str) {
  for (let i = 0; i < str.length; i++) {
    if (str[i] < 'A' || str[i] > 'Z') {
      return false;
    }
  }
  return true;
} 

const str1 = "AMANPAREEK";
isAllUppercaseUsingForLoop(str1);  //output: true 

const str2 = "Aman";
isAllUppercaseUsingForLoop(str2);  //output: false 

Solution 5: Using String.prototype.match

function isAllUppercaseUsingMatch(str) {
  return str.match(/^[A-Z]+$/) !== null;
} 

const str1 = "AMANPAREEK";
isAllUppercaseUsingMatch(str1);  //output: true 

const str2 = "Aman";
isAllUppercaseUsingMatch(str2);  //output: false 

Solution 6: Using String.prototype.split and every

function isAllUppercaseUsingSplitEvery(str) {
  return str.split('').every(char => char >= 'A' && char <= 'Z');
} 

const str1 = "AMANPAREEK";
isAllUppercaseUsingSplitEvery(str1);  //output: true 

const str2 = "Aman";
isAllUppercaseUsingSplitEvery(str2);  //output: false 

Solution 7: Using Array.prototype.reduce

function isAllUppercaseUsingReduce(str) {
  return [...str].reduce((allUppercase, char) => allUppercase && (char >= 'A' && char <= 'Z'), true);
} 

const str1 = "AMANPAREEK";
isAllUppercaseUsingReduce(str1);  //output: true 

const str2 = "Aman";
isAllUppercaseUsingReduce(str2);  //output: false 

Solution 8: Using String.prototype.includes

function isAllUppercaseUsingIncludes(str) {
  return !/[a-z]/.test(str);
} 

const str1 = "AMANPAREEK";
isAllUppercaseUsingIncludes(str1);  //output: true 

const str2 = "Aman";
isAllUppercaseUsingIncludes(str2);  //output: false 

Solution 9: Using String.prototype.replace

function isAllUppercaseUsingReplace(str) {
  return str.replace(/[A-Z]/g, '') === '';
} 

const str1 = "AMANPAREEK";
isAllUppercaseUsingReplace(str1);  //output: true 

const str2 = "Aman";
isAllUppercaseUsingReplace(str2);  //output: false 

Solution 10: Using for...of Loop

function isAllUppercaseUsingForOf(str) {
  for (const char of str) {
    if (char < 'A' || char > 'Z') {
      return false;
    }
  }
  return true;
} 

const str1 = "AMANPAREEK";
isAllUppercaseUsingForOf(str1);  //output: true 

const str2 = "Aman";
isAllUppercaseUsingForOf(str2);  //output: false 

Solution 11: Using ASCII Values

function isAllUppercaseUsingASCII(str) {
  for (let i = 0; i < str.length; i++) {
    const charCode = str.charCodeAt(i);
    if (charCode < 65 || charCode > 90) {
      return false;
    }
  }
  return true;
} 

const str1 = "AMANPAREEK";
isAllUppercaseUsingASCII(str1);  //output: true 

const str2 = "Aman";
isAllUppercaseUsingASCII(str2);  //output: false 

Popular Solutions