Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Convert Characters to ASCII Code using JavaScript?
ASCII stands for American Standard Code for Information Interchange, which is a method used for encoding characters by assigning them to a specific numerical value. The numerical values thus assigned are known as ASCII codes and are extensively utilized in computer systems to represent various characters including letters, numbers, punctuation marks, and special control characters.
Using charCodeAt() Method
The charCodeAt() method returns the ASCII code of the character at a specified index in a string. This is the most common method for ASCII conversion.
Syntax
string.charCodeAt(index)
Example
<!DOCTYPE html>
<html>
<head>
<title>Character to ASCII Converter</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f5f5f5;
margin: 0;
padding: 20px;
text-align: center;
}
.container {
max-width: 400px;
margin: 0 auto;
background-color: #fff;
padding: 20px;
border-radius: 5px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
input[type="text"] {
width: 100%;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 3px;
box-sizing: border-box;
font-size: 16px;
}
button {
width: 100%;
padding: 10px;
background-color: #4caf50;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
font-size: 16px;
}
p {
margin-top: 10px;
font-size: 18px;
}
</style>
</head>
<body>
<div class="container">
<h1>Character to ASCII Converter</h1>
<input type="text" id="inputText" placeholder="Enter a character">
<button onclick="convert()">Convert</button>
<p id="output"></p>
</div>
<script>
function convert() {
const input = document.getElementById("inputText").value;
if (input.length > 0) {
const asciiCode = input.charCodeAt(0);
document.getElementById("output").textContent = `ASCII code: ${asciiCode}`;
} else {
document.getElementById("output").textContent = "Please enter a character";
}
}
</script>
</body>
</html>
This function takes a string and index as parameters and returns the character's ASCII code at that index. When you enter a character and click "Convert", the JavaScript retrieves the character and displays its ASCII code.
Simple charCodeAt() Examples
// Basic ASCII conversion examples
console.log("A".charCodeAt(0)); // 65
console.log("a".charCodeAt(0)); // 97
console.log("0".charCodeAt(0)); // 48
console.log(" ".charCodeAt(0)); // 32 (space)
console.log("@".charCodeAt(0)); // 64
// Converting multiple characters
let word = "Hello";
for (let i = 0; i < word.length; i++) {
console.log(`${word[i]}: ${word.charCodeAt(i)}`);
}
65 97 48 32 64 H: 72 e: 101 l: 108 l: 108 o: 111
Using codePointAt() Method
The codePointAt() method returns the Unicode code point value of the character at a specified index. It handles Unicode characters better than charCodeAt().
Syntax
string.codePointAt(index)
Example
// Basic Unicode code point examples
console.log("A".codePointAt(0)); // 65
console.log("?".codePointAt(0)); // 8364
console.log("?".codePointAt(0)); // 128640
// Comparing charCodeAt vs codePointAt
let emoji = "?";
console.log("charCodeAt:", emoji.charCodeAt(0)); // 55357 (first part of surrogate pair)
console.log("codePointAt:", emoji.codePointAt(0)); // 127881 (full Unicode code point)
65 8364 128640 charCodeAt: 55357 codePointAt: 127881
Comparison of Methods
| Method | Best For | Range | Handles Emojis |
|---|---|---|---|
charCodeAt() |
ASCII characters | 0-65535 | No (returns partial) |
codePointAt() |
All Unicode characters | 0-1114111 | Yes (full code point) |
Practical Use Cases
// Check if character is uppercase
function isUppercase(char) {
let code = char.charCodeAt(0);
return code >= 65 && code <= 90;
}
console.log(isUppercase('A')); // true
console.log(isUppercase('a')); // false
// Convert string to ASCII array
function stringToAscii(str) {
return str.split('').map(char => char.charCodeAt(0));
}
console.log(stringToAscii("Hello")); // [72, 101, 108, 108, 111]
true false [72, 101, 108, 108, 111]
Conclusion
Use charCodeAt() for basic ASCII characters and codePointAt() for full Unicode support. Both methods are essential for character encoding tasks in JavaScript applications.
