Home >>JavaScript String Methods >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 | Description |
---|---|
index | This is a required parameter. It defines an integer representing the index of the character to return. |
Method | Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|---|
charAt() | Yes | Yes | Yes | Yes | Yes |
<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>
ABHIMANYU
<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>
ABHIMANYU