How To Check If A String Is All Uppercase In JavaScript

Check if a String is all Uppercase in JavaScript

Hello everyone. To continue the topic of working with strings in JavaScript, today we will discuss about how to check if a string is all uppercase in JavaScript. We have 3 methods: using test(), using toUpperCase() and using exec().

Check if a string is all uppercase in JavaScript

Below are the simple ways we want to introduce to you. These ways work well, even on old browser versions.

Using test()

You need a little knowledge of regex before using this. This method’s syntax and usage can be found here. In fact, the regex we will use to check is: /^[^a-z]+$/, which is all strings that does not contain letters from a to z (called lowercase).

First, we use the method test() on the regex we have declared and get the result of whether the test String matches the pattern. If yes, the function test() will return true. Otherwise, it will return false.

// Define a string to check
let string = "LEARN SHARE IT"

// Check if a string is all uppercase
console.log(/^[^a-z]+$/.test(string))

Output:

true

Another example:

// Define a string to check
let string = "learn Share IT"

// Check if a string is all uppercase
console.log(/^[^a-z]+$/.test(string))

Output:

false

As can be seen, in the first example, all letters are uppercase so the method returns true. However, in the second one, the string is not all uppercase so the output is false. In the next section, we will use a method that works in the same way as this one.

Using exec()

Syntax:

/regex/.exec(string)

Parameter:

  • string: the string to check if is all uppercase.

This method exec() checks whether a string matches with a given regex. However an array of matched substrings is returned if the string matched, otherwise null is returned. For example:

// Define a string to check
let string = "LearnShareIT"

// Check if a string is all uppercase
console.log(/^[^a-z]+$/.exec(string)!=null)

Output:

false

Another example:

// Define a string to check
let string = "LEARNSHAREIT"

// Check if a string is all uppercase
console.log(/^[^a-z]+$/.exec(string)!=null)

Output:

true

The logic of this approach is somehow simple to acknowledge. As explained, this function works like the test() function; when the provided string matches with the regex we passed, then it returns an array. Otherwise, null will be returned instead. Therefore, we just need to check if the returned value is not null or not as if it is not, the string is all uppercase.

Using toUppercase()

Syntax:

toUppercase(string)

Parameter:

  • string: the string to check if is all uppercase.

This method will convert a given string to uppercase format. Because of that, we can use this function to get the uppercase string from it and see if our string is equal to this uppercase one or not. If it is strictly equaled, then our string is all uppercase already.

// Define a string to check
let string = "LearnShareIT"

// Check if a string is all uppercase
console.log(string.toUpperCase()==string)

Output:

false

Another example:

// Define a string to check
let string = "LEARNSHAREIT"

// Check if a string is all uppercase
console.log(string.toUpperCase()==string)

Output:

true

It is the fact that many programmers tend to use this method because they don’t have to deal with regex expressions. We also recommend you use this approach if you are new to JavaScript.

Summary

We have discussed many ways to check if a string is all uppercase in JavaScript. We suggest you use the third approach as it is the quickest and simplest one. Have a good day!

Maybe you are interested:

Leave a Reply

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