Home >>JavaScript String Methods >JavaScript String charCodeAt() Method

JavaScript String charCodeAt() Method

JavaScript String charCodeAt() Method

JavaScript charCodeAt() method is used to get the Unicode of the character at the specified index in a given input string. The index value starts from 0 and goes to n-1, where n is the total length of the given string.

Syntax:
string.charCodeAt(index);

Parameter Values

Parameter Description
index This is a required parameter. It defines a number representing the index of the character to return.

Browser Support

Method Chrome Edge Firefox Safari Opera
charCodeAt() Yes Yes Yes Yes Yes
Here is an example of charCodeAt() method:
<html>
<body>
<p>JERRY</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
  var str = "JERRY";
  var n = str.charCodeAt(0);
  document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>
Output:

JERRY

Example 2:
<html>
<body>
<p>Jerry</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
  var str = "Jerry";
  var n = str.charCodeAt(str.length-1);
  document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>
Output:

Jerry


No Sidebar ads