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> |
chevron_right
filter_none
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> |
chevron_right
filter_none
Output:
- Before clicking on the button:

- After clicking on the button:

Recommended Posts:
- How to replace lowercase letters with an other character in JavaScript ?
- JavaScript | Set Date Methods
- JavaScript | Get Date Methods
- JavaScript | Object Methods
- JavaScript | Array Iteration Methods
- JavaScript Basic Array Methods
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- HTTP GET and POST Methods in PHP
- jQuery | Selectors and Event Methods
- jQuery UI | draggable() and droppable() methods
- jQuery | Animation, Slide methods with Examples
- JavaScript Course | Understanding Code Structure in JavaScript
- jQuery | Hide/Show, Toggle and Fading methods with Examples
- JavaScript Course | Logical Operators in JavaScript
- JavaScript Course | Printing Hello World in JavaScript
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.



