JavaScript Program to Determine the Unicode Code Point at a Given Index
It is important to know what the Unicode code points are before moving on to the JavaScript implementation. A Unicode code point is a distinct integer value that represents each character in a string. A programmer may easily manipulate strings using JavaScript’s techniques for working with these code points.
Table of Content
- Using the codePointAt() Method
- Using the charCodeAt() Method
- Using the fromCodePoint() Method
Using the codePointAt() Method
JavaScript provides the codePointAt() method for strings, which returns the Unicode code point of the character at a specified index. The index refers to the position of the character in the string, starting from 0.
Syntax:
string.codePointAt(index);
Example: The below code implements the codePointAt() method to get the Unicode point at a given index.
Javascript
let str = "Hello, World!" ; let result1 = str.codePointAt(0); let result2 = str.codePointAt(7); console.log( "Unicode point of character at index 0 in" , str, "is:" , result1); console.log( "Unicode point of character at index 7 in" , str, "is:" , result2); |
Unicode point of character at index 0 in Hello, World! is: 72 Unicode point of character at index 7 in Hello, World! is: 87
Using the charCodeAt() Method
The charCodeAt() method returns the code unit at a given index, while codePointAt() clearly gets the single-character Unicode code point. Remember that JavaScript strings employ UTF-16 encoding, and that certain characters that are not part of the Basic Multilingual Plane (BMP) call for two code units, or surrogate pairs, which are represented by separate charCodeAt() values.
Example: The below code uses the charCodeAt() method to determine unicode point at a given index.
Javascript
const str = "?w3wiki" ; const codePoint1 = str.codePointAt(0); const codeUnit1 = str.charCodeAt(0); const codeUnit2 = str.charCodeAt(3); console.log(codePoint1); console.log(codeUnit1); console.log(codeUnit2); |
119558 55348 101
Using the fromCodePoint() Method
The codePointAt() method is reversed by this technique. This function generates a string with the matching characters from an integer, or an array of integers, that represent Unicode code points.
Example: The below code implements the fromCodePoint() method to get unicode point t a given index.
Javascript
const codePoint = 128512; const emojiString = String.fromCodePoint(codePoint); console.log(emojiString); |
?
Practical Applications
Determining Unicode code points empowers various tasks:
- Character Analysis: Analyze language distribution by counting code points within specific ranges.
- Text Encoding: Convert text between different encodings like UTF-8 and UTF-16 based on code point values.
- Font Development: Ensure fonts correctly display characters by mapping code points to glyphs.
- Emojis and Special Characters: Work with emojis and special characters effectively by using their code points.