Skip to content
the poor coder

look up asciii value javascript

Poor Man
Published
Reading time
2 min read
On this page

Looking Up ASCII Values in JavaScript#

If you're working with strings in JavaScript, you may need to find the ASCII value of a particular character. The ASCII (American Standard Code for Information Interchange) table assigns a numerical value to each letter, number, and symbol that can be represented on a computer.

Method 1: Using the charCodeAt() Method#

One way to find the ASCII value of a character in JavaScript is to use the charCodeAt() method. This method returns the Unicode value (which is the same as the ASCII value for characters in the basic ASCII range) of the character at a specified index in a string. Here's an example:

JavaScript

const myString = "Hello, World!";
const myChar = myString.charCodeAt(0);

console.log(myChar); // Output: 72

In this example, we're assigning the string "Hello, World!" to a variable called myString. We then use the charCodeAt() method to find the ASCII value of the first character in the string (which is the letter "H"). The ASCII value of "H" is 72, so that's what gets logged to the console.

Method 2: Using the fromCharCode() Method#

Another way to work with ASCII values in JavaScript is to use the fromCharCode() method. This method takes one or more character codes and returns the corresponding string. Here's an example:

JavaScript

const myChar = String.fromCharCode(72);

console.log(myChar); // Output: "H"

In this example, we're using the fromCharCode() method to convert the ASCII value 72 to the corresponding character (which is "H"). We then assign that character to a variable called myChar and log it to the console.

Using Highlight.js for Syntax Highlighting#

To make the code examples in this post easier to read, I'm using the Highlight.js library for syntax highlighting. This library adds CSS classes to code blocks that can be styled to highlight different programming languages. Here's an example of how to use Highlight.js with a <pre> and <code> block:

HTML

<pre><code class="language-javascript">
const myString = "Hello, World!";
const myChar = myString.charCodeAt(0);

console.log(myChar); // Output: 72
</code></pre>

In this example, we're wrapping our code block in a <pre> tag and a <code> tag with the class "language-javascript". This tells Highlight.js to apply JavaScript syntax highlighting to the code block.

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