Friday, November 21, 2025
HomeLanguagesJavascriptHow to process each letter of text using JavaScript ?

How to process each letter of text using JavaScript ?

Given a string and the task is to process it character by character. 

  • JavaScript String toUpperCase() Method: This method converts a string to uppercase letters. 
    Syntax: 
string.toUpperCase()
  • Parameters: This method accepts single parameter str which is required. It specifies the string to be searched.
    Return Value: It returns a string denoting the value of a string converted to uppercase. 
     
  • JavaScript String charAt() Method: This method returns the character at the passed index in a string. The index of characters starts from 0.
    Syntax: 
string.charAt(index)
  • Parameters: This method accepts single parameter index which is required. It specifies the integer representing the index of the character to return.
    Return Value: It returns a string, denoting the character at the passed index, or an empty string if the index number is invalid. 
     

Example 1: This example processes the string letter by letter using for loop and converts each letter to uppercase separately. 

html




<!DOCTYPE HTML>
<html>
    <head>
        <title>
            How to process each character of text
        </title>
    </head>
     
    <body style = "text-align:center;">
     
        <h1 style = "color:green;" >
            GeeksForGeeks
        </h1>
     
        <p id = "GFG_UP" style =
            "font-size: 15px; font-weight: bold;">
        </p>
 
         
        <button onclick = "gfg_Run()">
            check
        </button>
         
        <p id = "GFG_DOWN" style =
            "color:green; font-size: 20px; font-weight: bold;">
        </p>
 
         
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
            var str = "This is String";
            el_up.innerHTML = "String = '"+str + "'";
         
            function gfg_Run() {
                var str_Upper = "";
                for (var i = 0; i < str.length; i++) {
                    str_Upper += str.charAt(i).toUpperCase();
                }
                el_down.innerHTML = str_Upper;
            }        
        </script>
    </body>
</html>                   


Output: 

  • Before clicking on the button: 

  • After clicking on the button: 

Example 2: This example processes the string letter by letter using for in loop and alerts each letter separately. 

html




<!DOCTYPE HTML>
<html>
    <head>
        <title>
            How to process each letter of text
        </title>
    </head>
     
    <body style = "text-align:center;">
     
        <h1 style = "color:green;" >
            GeeksForGeeks
        </h1>
     
        <p id = "GFG_UP" style =
            "font-size: 15px; font-weight: bold;">
        </p>
 
         
        <button onclick = "gfg_Run()">
            check
        </button>
         
        <script>
            var el_up = document.getElementById("GFG_UP");
            var str = "str";
            el_up.innerHTML = "String = '" + str + "'";
         
            function gfg_Run() {
                for (var i in str) {
                    alert(" character = "+str.charAt(i));
                }
            }        
        </script>
    </body>
</html>                   


Output: 

  • Before clicking on the button: 

  • After clicking on the button: 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32405 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6781 POSTS0 COMMENTS
Nicole Veronica
11928 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11995 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7165 POSTS0 COMMENTS
Thapelo Manthata
6862 POSTS0 COMMENTS
Umr Jansen
6847 POSTS0 COMMENTS