Check string contains uppercase javascript

You are here: Home / JavaScript / Check if a String Contains Uppercase Letters in JavaScript

We can check if a string contains uppercase characters in JavaScript by checking each letter to see if that letter is uppercase in a loop. We will make use of the toUpperCase() and charAt() methods. Here is our function that will check if there are any uppercase letters in a string.

function checkUppercase(str){
    for (var i=0; i<str.length; i++){
      if (str.charAt(i) == str.charAt(i).toUpperCase() && str.charAt(i).match(/[a-z]/i)){
        return true;
      }
    }
    return false;
};

Notice we use a regular expression above to make sure the character is a letter.

Here is our function with a simple example:

function checkUppercase(str){
    for (var i=0; i<str.length; i++){
      if (str.charAt(i) == str.charAt(i).toUpperCase() && str.charAt(i).match(/[a-z]/i)){
        return true;
      }
    }
    return false;
};

console.log(checkUppercase("all letters here are lowercase"));
console.log(checkUppercase("We Have some uppercase Letters in this One."));

#Output:
false
true

When processing strings in a program, it can be useful to know if we have uppercase or lowercase characters. Using JavaScript, we can easily check if a string contains uppercase letter(s) with the help of the JavaScript toUpperCase() method.

To check if a string contains uppercase, we just need to loop over all letters in the string until we find a letter that is equal to that letter after applying the toUpperCase() method and make sure that character is a letter.

Below is our JavaScript function again which will check if a string contains an uppercase letter.

function checkUppercase(str){
    for (var i=0; i<str.length; i++){
      if (str.charAt(i) == str.charAt(i).toUpperCase() && str.charAt(i).match(/[a-z]/i)){
        return true;
      }
    }
    return false;
};

We can also check if a string contains lowercase characters in JavaScript very easily.

To check if a string contains lowercase letters in JavaScript, we can adjust our function that we defined above to use the JavaScript toLowerCase() method, instead of the toUpperCase() method.

Below is a JavaScript function that will check if a string contains a lowercase letter.

function checkLowercase(str){
    for (var i=0; i<str.length; i++){
      if (str.charAt(i) == str.charAt(i).toLowerCase() && str.charAt(i).match(/[a-z]/i)){
        return true;
      }
    }
    return false;
};
function checkLowercase(str){
    for (var i=0; i<str.length; i++){
      if (str.charAt(i) == str.charAt(i).toLowerCase() && str.charAt(i).match(/[a-z]/i)){
        return true;
      }
    }
    return false;
};

console.log(checkLowercase("ALL THE LETTERS ARE UPPERCASE"));
console.log(checkLowercase("We Have some uppercase Letters in this One."));

#Output:
false
true

Hopefully this article has been useful for you to learn how to check if a string contains uppercase letters JavaScript.

  • 1.  JavaScript Check If Number is a Whole Number
  • 2.  Check if Checkbox is Checked in JavaScript
  • 3.  Using JavaScript to Change Text of Element
  • 4.  How to Check if a Number is Even or Odd in JavaScript
  • 5.  charAt() JavaScript – Getting the Index Position of a Character
  • 6.  Using JavaScript to Remove Character From String
  • 7.  JavaScript Opacity – How to Change the Opacity of an Element Using JavaScript
  • 8.  cleartimeout JavaScript – How the clearTimeout() Method in JavaScript Works
  • 9.  Using JavaScript to Capitalize the First Letter of a String
  • 10.  JavaScript indexOf – Get the position of a value within a string

Check string contains uppercase javascript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

The best way is to use a regular expression, a ternary operator, and the built in .test() method for strings.

I leave you to Google the ins and outs of regular expressions and the test method for strings (they're easy to find), but here we'll use it to test your variable.

/[a-z]/i.test(your-character-here)

This will return TRUE of FALSE based on whether or not your character matches the character set in the regular expression. Our regular expression checks for all letters a-z /[a-z]/ regardless of their case thanks to the i flag.

So, a basic test would be:

var theAnswer = "";
if (/[a-z]/i.test(your-character-here)) {
  theAnswer = "It's a letter."
}

Now we need to determine if it's upper or lower case. So, if we remove the i flag from our regular expression, then our code above will test for lower case letters a-z. And if we stick another if statement in the else of our first if statement, we can test for upper case too by using A-Z. Like this:

var theAnswer = "";
if (/[a-z]/.test(your-character-here)) {
  theAnswer = "It's a lower case letter."
} else if (/[A-Z]/.test(your-character-here)) {
  theAnswer = "It's an upper case letter.";
}

And just in case it's not a letter, we can add a final else statement:

var theAnswer = "";
if (/[a-z]/.test(your-character-here)) {
  theAnswer = "It's a lower case letter."
} else if (/[A-Z]/.test(your-character-here)) {
  theAnswer = "It's an upper case letter.";
} else {
  theAnswer = "It's not a letter."
}

The above code would work. But it's kinda ugly. Instead, we can use a "ternary operator" to replace our if-else statements above. Ternary operators are just shorthand simple ways of coding an if-else. The syntax is easy:

(statement-to-be-evaluated) ? (code-if-true) : (code-if-false)

And these can be nested within each other, too. So a function might look like:

var theAnswer = "";
function whichCase(theLetter) {
  theAnswer = /[a-z]/.test(theLetter) ? "It's lower case." : "";
  theAnswer = /[A-Z]/.test(theLetter) ? "It's upper case." : "";
  return(theAnswer);
}

The above code looks good, but won't quite work, because if our character is lower case, theAnswer gets set to "" when it test for uppercase, so lets nest them:

var theAnswer = "";
function whichCase(theLetter) {
  theAnswer = /[a-z]/.test(theLetter) ? "It's lower case." : (/[A-Z]/.test(theLetter) ? "It's upper case." : "It's not a letter.");
  return(theAnswer);
}

That will work great! But there's no need to have two seperate lines for setting the variable theAnswer and then returning it. And we should be using let and const rather than var (look those up if you're not sure why). Once we make those changes:

function whichCase(theLetter) {
  return(/[A-Z]/.test(theLetter) ? "It's upper case." : (/[a-z]/.test(theLetter) ? "It's lower case." : "It's not a letter.")); 
}

And we end up with an elegant, concise piece of code. ;)

How do you check if all strings are uppercase?

To check if a string is in uppercase, we can use the isupper() method. isupper() checks whether every case-based character in a string is in uppercase, and returns a True or False value depending on the outcome.

How do you know if a string is uppercase or lowercase?

Traverse the string character by character from start to end. Check the ASCII value of each character for the following conditions: If the ASCII value lies in the range of [65, 90], then it is an uppercase letter. If the ASCII value lies in the range of [97, 122], then it is a lowercase letter.

How do you check if a string is all lowercase Javascript?

log(str. toUpperCase() === str. toLowerCase()); If we know that the string is uppercase and it's not equal to it's lowercase variant, then we have an all uppercase string.

How do you check if a string contains any special characters in Javascript?

To check if a string contains special characters, call the test() method on a regular expression that matches any special character. The test method will return true if the string contains at least 1 special character and false otherwise.