• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Check if a String Contains Uppercase Letters in JavaScript

Check if a String Contains Uppercase Letters in JavaScript

May 18, 2022 Leave a Comment

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;
};

How to Check if a String Contains Lowercase letters in JavaScript

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.

Other Articles You'll Also Like:

  • 1.  JavaScript Change Background Color of Element
  • 2.  Remove All Instances of Value From an Array in JavaScript
  • 3.  Remove Undefined Values From an Array in JavaScript
  • 4.  Using JavaScript to Check a Radio Button
  • 5.  Using JavaScript to Round to 3 Decimal Places
  • 6.  Using JavaScript to Sort an Array of Strings
  • 7.  Remove Word From String In JavaScript
  • 8.  parseFloat JavaScript – How to use the JavaScript parseFloat() method
  • 9.  Using the appendChild Method with JavaScript to Add Items to a List
  • 10.  Using JavaScript to Get the Decimal Part of a Number

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

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy