Method 1: Using the charCodeAt() Method
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
fromCharCode() method. This method takes one or more character codes and returns the corresponding string. Here's an example:
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
In this example, we're wrapping our code block in a
<pre> tag and a