JavaScript

How to Get The Last Character Of a String in Javascript

In this tutorial, we are going to see how to get the last character of a string in Javascript using the charAt() function which returns the character at the index specified in a string in JavaScript.
 

Program to get the last character of a string
<!DOCTYPE html> 
<html> 
<head></head> 
<body> 
    <h4>StackHowTo</h4> 
    <p>Click the button to display the last character</p> 
    <button onclick="getLastChar()">Click here!</button> 
    <b><p id="char"></p></b> 
    <script> 
        function getLastChar() { 
            var str = "StackHowTo"; 
            var lastC = str.charAt(str.length-1); 
            document.getElementById("char").innerHTML = lastC; 
        } 
    </script> 
</body> 
</html>
[st_adsense]
Result

StackHowTo

Click the button to display the last character

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More [st_adsense]

Leave a Reply

Your email address will not be published. Required fields are marked *