此方法返回一个数字,指示给定索引处字符的Unicode值。

Unicode码位的范围从0到1,114,111。前128个Unicode码位是ASCII字符编码的直接匹配。charCodeAt()始终返回小于65536的值。

charCodeAt() - 语法

string.charCodeAt(index);

index   -   小于字符串长度的0到1之间的整数;如果未指定,则默认为0。

charCodeAt() - 返回值

返回一个数字,该数字指示给定索引处字符的Unicode值。如果给定索引不在小于字符串长度的0到1之间,则返回NaN。

charCodeAt() - 示例

<html>
   <head>
      <title>JavaScript String charCodeAt() Method</title>
   </head>
   
   <body>   
      <script type="text/javascript">
         var str=new String( "This is string" );
         document.write("str.charCodeAt(0) is:" + str.charCodeAt(0)); 
         document.write("<br />str.charCodeAt(1) is:" + str.charCodeAt(1)); 
         document.write("<br />str.charCodeAt(2) is:" + str.charCodeAt(2)); 
         document.write("<br />str.charCodeAt(3) is:" + str.charCodeAt(3)); 
         document.write("<br />str.charCodeAt(4) is:" + str.charCodeAt(4)); 
         document.write("<br />str.charCodeAt(5) is:" + str.charCodeAt(5)); 
      </script>      
   </body>
</html>

运行上面代码输出

str.charCodeAt(0) is:84
str.charCodeAt(1) is:104
str.charCodeAt(2) is:105
str.charCodeAt(3) is:115
str.charCodeAt(4) is:32
str.charCodeAt(5) is:105 

参考链接

https://www.learnfk.com/javascript/string-charcodeat.html