Javascript examples for String:charAt
The charAt() method returns the character at the specified index in a string.
The index of the first character is 0.
Parameter | Description |
---|---|
index | Required. An integer representing the index of the character you want to return |
A String, representing the character at the specified index, or an empty string if the index number is not found
The following code shows how to return the first character of a string:
<!DOCTYPE html> <html> <body> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {//from w w w . jav a2 s .co m var str = "HELLO WORLD"; var res = str.charAt(str.length-1); document.getElementById("demo").innerHTML = res; } </script> </body> </html>