Return the last character of a string: - Javascript String

Javascript examples for String:charAt

Description

Return the last 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.j av a  2 s  .  c om*/
    var str = "HELLO WORLD";
    var res = str.charAt(str.length-1);
    document.getElementById("demo").innerHTML = res;
}
</script>

</body>
</html>

Related Tutorials