JavaScript methods to increment/decrement alphabetical letters

The task is to increment/decrement alphabetic letters using ASCII value with the help of JavaScript.

Approach:

  • Use charCodeAt() method to return the Unicode of the character at the specified index in a string
  • Increment/Decrement the ASCII value depending on the requirement.
  • Use fromCharCode() method to convert Unicode values into characters.

Example 1: In this example, the character ‘a’ is incremented by clicking on the button.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
  
<head
    <title
        JavaScript method to increment/
        decrement letters
    </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_Fun()">
        click here
    </button>
      
    <p id = "GFG_DOWN" style
        "color:green; font-size: 20px; font-weight: bold;">
    </p>
      
    <script
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
          
        up.innerHTML = "Click on the button to increment "
                    + "the letter.";
        down.innerHTML = 'a';
          
        function nextCharacter(c) {
            return String.fromCharCode(c.charCodeAt(0) + 1);
        }
          
        function GFG_Fun() {
            var c = down.innerHTML;
            down.innerHTML = nextCharacter(c);
        }
    </script
</body
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

Example 2: In this example, the character ‘z’ is decremented by clicking on the button.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML> 
<html
  
<head
    <title
        JavaScript method to increment/
        decrement letters
    </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_Fun()">
        click here
    </button>
      
    <p id = "GFG_DOWN" style
        "color:green; font-size: 20px; font-weight: bold;">
    </p>
      
    <script
        var up = document.getElementById('GFG_UP');
        var down = document.getElementById('GFG_DOWN');
          
        up.innerHTML = "Click on the button to decrement "
                    + "the letter.";
        down.innerHTML = 'z';
          
        function nextCharacter(c) {
            return String.fromCharCode(c.charCodeAt(0) - 1);
        }
          
        function GFG_Fun() {
            var c = down.innerHTML;
            down.innerHTML = nextCharacter(c);
        }
    </script
</body
  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.