Check If letter In String Is Uppercase Or Lowercase In Javascript

Check if letter in String is Uppercase or Lowercase in JS

In this article, we will show you how to check if letter in string is uppercase or lowercase in Javascript. Some ways will be mentioned, for example, using toUpperCase() with toLowerCase() or using the charCodeAt() method.

How to check if letter in string is uppercase or lowercase in Javascript

If you need to check if letter in string is uppercase or lowercase to validate the string, please read the following sections of the article. We’ll show you how to do this in detail.

Using toUpperCase() with toLowerCase()

The toUpperCase() method converts all characters in a string to uppercase. You can use this method to get a string to compare with the string to be checked.

Code:

import React, { useEffect, useState } from "react";

const App = () => {
    const [check, setCheck] = useState("");
    const string = "LearnShareIT";

    useEffect(() => {
        const character = string.charAt(5);

        if (character === character.toUpperCase()) {
            setCheck("This character is uppercase");
        }

        if (character === character.toLowerCase()) {
            setCheck("This character is lowercase");
        }
    }, []);

    return (
        <>
            <h2>Check if letter in String is Uppercase or Lowercase in JS | LearnShareIT</h2>
            <hr />
            <h3>{check}</h3>
        </>
    );
};

export default App;

Output:

Here we will check the fifth character in the string “LearnShareIT” to see if it is a capital letter. We will use the toUpperCase() method with the character to be checked so that there is a character for comparison. If two values ​​are equal, then the character is in uppercase, similar to toLowerCase().

Using charCodeAt() method

The charCodeAt() method returns the Unicode value of a character at a specified position in the string.

Syntax:

string.charCodeAt(index)

Parameter:

  •  index: is the index of the character that you want its Unicode value to be returned.

But with the Unicode system, we will have specific milestones to distinguish between uppercase and lowercase values. We can use this to write code to check characters in Unicode.

Code:

import React, { useEffect, useState } from "react";

const App = () => {
    const [check, setCheck] = useState("");
    const string = "LearnShareIT";
    useEffect(() => {
        if (string.charCodeAt(5) >= 65 && string.charCodeAt(5) <= 90) {
            setCheck("This character is uppercase");
        } else {
            setCheck("This character is lowercase");
        }
    }, []);

    return (
        <>
            <h2>Check if letter in String is Uppercase or Lowercase in JS | LearnShareIT</h2>
            <hr />
            <h3>{check}</h3>
        </>
    );
};

export default App;

The return result of this method is the same as using toUpperCase() to check. We will check the character value in the fifth position using charAt() and see if it is more than 65 and less than 90. If the return value is actual, then we can know that the character is an uppercase character and vice versa.

Summary

To sum up, you already know how to check if letter in string is uppercase or lowercase in Javascript, but we recommend using toUpperCase() in combination with toLowerCase() because it can check quickly.

Maybe you are interested:

Leave a Reply

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