ใช้ String.fromCharCode() แปลงรหัส Unicode เป็นตัวอักษร
Syntax: String.fromCharCode(yourIntegerValue)
ตัวอย่างโค้ด
var letters = 'ABCDEFGH';
for (var i = 0; i < letters.length; i++) {
console.log(letters[i] + "=" + letters.charCodeAt(i));
}
var number = 72;
console.log(number + " character code is=" + String.fromCharCode(number));รันด้วยคำสั่ง:
node fileName.js
ชื่อไฟล์: demo265.js
ผลลัพธ์
PS C:\Users\Amit\javascript-code> node demo265.js A=65 B=66 C=67 D=68 E=69 F=70 G=71 H=72 72 character code is=H
คำอธิบาย: charCodeAt() ดึงรหัส Unicode ของตัวอักษร String.fromCharCode() ทำตรงข้าม แปลงรหัสเป็นตัวอักษร รองรับหลายรหัสพร้อมกันได้: String.fromCharCode(72, 101, 108, 108, 111) → "Hello"