JavaScript charCodeAt method entry instance (used to get the Unicode encoding for the specified location character)

  • 2020-03-30 04:09:12
  • OfStack

JavaScript charCodeAt method

The charCodeAt method is used to get the Unicode encoding for the specified position character from a string. The syntax is as follows:


str_object.charCodeAt( x )

Parameter description:

parameter instructions
str_object String (object) to manipulate
x A necessity. A number that represents a position

Tip: strings are counted from 0.

Example of a charCodeAt method


<script language="JavaScript">
document.write( "jb51".charCodeAt( 1 ) );
</script>

Run the example and output:


98

This method returns NaN if the parameter x is not between 0 and the maximum length of the string.

The charCodeAt method is very similar to the charAt method, except that the former returns the encoding of the character in the specified position, while the latter returns a substring of the character.


Related articles: