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.
<!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> |
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.
<!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> |
Output:
- Before clicking on the button:

- After clicking on the button:



