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

JavaScript String charAt() Method

JavaScript String charAt() Method

JavaScript charAt() method is used to get the character present at any specified index. The index value should lie between 0 and length of the string -1.

Syntax:
string.charAt(index);

Parameter Values

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

Browser Support

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

ABHIMANYU

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

ABHIMANYU


No Sidebar ads