String charAt() Method - Javascript String

Javascript examples for String:charAt

Description

The charAt() method returns the character at the specified index in a string.

The index of the first character is 0.

Parameter Values

Parameter Description
index Required. An integer representing the index of the character you want to return

Return Value:

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:

Demo Code

ResultView the demo in separate window

<!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>

Related Tutorials