JavaScript String charCodeAt()"

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

charCodeAt() method in javascript that returns the integer representing the Unicode value of the character whose position in the string is passed as the argument of this method.

Syntax of String.prototype.charCodeAt() in JavaScript

The syntax of the charCodeAt() method in javascript is:

Here, str is the string, and index represents the position of the character in the string whose Unicode encoding is required.

The index of the first character is 0, the second character is 1, etc. The index of the last character is string size - 1.

Parameters of charCodeAt() in JavaScript

The charCodeAt() method accepts only one argument which is the index of the character in the string whose Unicode encoding is required. The index can range from 0 to string length - 1.

Return Value of String.prototype.charCodeAt() in JavaScript

The charCodeAt() method returns the character's Unicode, which ranges from 0 to 65535. This method returns NaN (Not-a-Number) if the argument passed is outside the range of the string which is 0 to string length - 1.

Exceptions of charCodeAt() in JavaScript

The charCodeAt() method return NaN (Not-a-Number) when the index provided as the argument lies outside the range of the string length i.e. if the index is negative or the index is greater than or equal to the string length.

Example

Output :

Explanation:

charCodeAt() extracts the character present at position 6 i.e. it extracts 'W' and then prints the Unicode integer of 'W' which is 87.

More Examples

  • When the index passed is valid i.e. it lies in the range of the string length which is 0 to string length - 1, then charcodeat javascript method return its unicode encoding.

Output :

Explanation :

charCodeAt() extracts the character present at position 4 i.e it extracts 'o' and then it prints the Unicode integer of 'o' which is 111.

  • When the index passed is not valid i.e. it lies outside the range of the string length i.e. if the index is negative or the index is greater than or equal to string length, then charcodeat JavaScript method returns NaN.

Output :

Explanation:

Since the index passed i.e. 12 is greater than or equals string length which is not a valid index therefore charcodeat javascript method returns NaN.

Supported Browsers

All accepted browsers:

  • Google Chrome 1 and higher versions.
  • Edge 12 and higher versions.
  • using Firefox 1 and higher versions.
  • Safari 1 and higher versions.
  • Internet Explorer 4 and higher versions.
  • Opera 4 and higher versions.

Conclusion

  • Unicode is an information technology standard that is used to encode characters into integers.
  • charcodeat javascript method returns a character's Unicode whose index is passed as the argument to this method.
  • charcodeat javascript method returns NaN if the index passed is not valid i.e. if the index is negative or the index is greater than or equal to string length.