Skip to content
the poor coder

javascript character ascii value modify

Poor Man
Published
Reading time
2 min read

Javascript Character ASCII Value Modify#

If you want to modify the ASCII value of a character in Javascript, you can use the charCodeAt() and fromCharCode() methods. Let's see an example:

JavaScript

let str = "Hello World";
let asciiVal = str.charCodeAt(0); //Get ASCII value of first character (H)
let newAsciiVal = asciiVal + 1; //Add 1 to ASCII value
let newChar = String.fromCharCode(newAsciiVal); //Convert ASCII value to character

console.log(newChar); //Outputs "I"

In this example, we first declare a string variable "str" with the value "Hello World". We then use the charCodeAt() method to get the ASCII value of the first character in the string ("H"). We store this value in the variable "asciiVal".

Next, we add 1 to the ASCII value and store it in the variable "newAsciiVal". We then use the fromCharCode() method to convert the new ASCII value back into a character, which we store in the variable "newChar". Finally, we log this new character to the console.

There are multiple ways to modify ASCII values in Javascript. Another way is to use bitwise operators. Let's see an example:

JavaScript

let char = "a";
let newChar = String.fromCharCode(char.charCodeAt(0) ^ 32);

console.log(newChar); //Outputs "A"

In this example, we first declare a character variable "char" with the value "a". We then use the charCodeAt() method to get the ASCII value of this character. We then use a bitwise XOR operator (^) with the value 32 to flip the 6th bit (which changes the case of the character). We then use the fromCharCode() method to convert the new ASCII value back into a character, which we store in the variable "newChar". Finally, we log this new character to the console.

These are just two examples of ways to modify ASCII values in Javascript. There are many other methods and techniques you can use, depending on your specific needs and circumstances.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC