- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to fetch character from Unicode number - JavaScript?
In this article we are going to learn about how to fetch character from Unicode number JavaScript. Before we jump into the article let’s have a quick view on the Unicode number in JavaScript.
A Unicode escape sequence in JavaScript can be used to express identifiers and string literals in Unicode. A four-digit hexadecimal value for X indicates the basic syntax, which is uXXXX.
Let’s dive into the article to learn more about “How to fetch character from Unicode number JavaScript.” To retrieve character from Unicode number, use String.fromCharCode() in JavaScript
String.fromCharCode() method in JavaScript
The JavaScript string method charCodeAt() can be used to find the Unicode value for a character, that is present in a string at a particular location. The charCodeAt() method must be called through a specific instance of the String class because it is a method of the String object.
Syntax
Following is the syntax for String.fromCharCode()
string.charCodeAt([position]);
For getting better understanding, let’s look into the following examples
Example
Let’s look into the following example to know how to use CharCode() in JavaScript
<!DOCTYPE html> <html> <body> <script> var tutorial_string = 'ABCDEFGHI'; document.write(tutorial_string.charCodeAt(0)+"<br>"); document.write(tutorial_string.charCodeAt(1)+"<br>"); document.write(tutorial_string.charCodeAt(2)+"<br>"); document.write(tutorial_string.charCodeAt(3)+"<br>"); </script> </body> </html>
When the script gets executed, it will generate an output consisting of Unicode values obtained for the string, we used in the above script. This occurs when the event gets triggered in the script.
Example
Considering the another example, where we are getting the Unicode value for 2 character
<!DOCTYPE html> <html> <body> <p>Getting the Unicode Value:</p> <p id="tutorial"></p> <script> let text = "WELCOME"; let code = text.charCodeAt(2); document.getElementById("tutorial").innerHTML = code; </script> </body> </html>
On running the above script, the web-browser, triggers the event, and displays a text along with the Unicode value of the second character of the text we used in the script.
Example
Let’s look into the another example, where we are passing no parameter
<!DOCTYPE html> <html> <body> <p>Getting the Unicode Value:</p> <p id="tutorial"></p> <script> var tutorial_string = 'TutorialsPoint'; document.getElementById("tutorial").innerHTML = (tutorial_string.charCodeAt()); </script> </body> </html>
When the script gets executed, it will generate an output consisting of text along with a Unicode value printed on the webpage. Because we haven't specified any parameters to return values, the script checks for the parameter to return value as soon as it runs. If a parameter is not found by default, it will take parameter "0" and display a Unicode value.
Example
Execute the below code where we mentioned parameter is out of bonds
<!DOCTYPE html> <html> <body> <p>Getting the Unicode Value:</p> <p id="tutorial"></p> <script> var tutorial_string = 'The Best E-Way'; document.getElementById("tutorial").innerHTML = (tutorial_string.charCodeAt(22)); </script> </body> </html>
In this case, when the user executes the script, the event gets triggered, checks the string and parameter values, and returns the values. We have mentioned the parameter values more than the string count, which is out of bonds, so it will return the Unicode values as NaN.