Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • String methods charAt and charCodeAt in JS

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 488
    Comment on it

    charAt() method : This method returns the character exist in a specified index of string.The index in a string always starts with 0.

    Syntax of charAt() method :

    string.charAt(index)
    

    The index is required field. If no character is found or index is less then 0 then it will return NaN or empty string.

    Example of charAt() method : App.html

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the button to display the second character of the string "This is my home.".</p>
    
    <button onclick="showSecondChar()">Show Second Char</button>
    
    <p id="container"></p>
    
    <script>
    function showSecondChar() {
        var str = "This is my home.";
        var result = str.charAt(1);
        document.getElementById("container").innerHTML = result;
    }
    </script>
    </body>
    </html>
    
    **Output :** h
    

    charCodeAt() method : This method is used to return the unicode value of a character at specified index of the string. In this case index also starts with 0.

    Syntax of mehod :

    string.charCodeAt(index)
    

    The index is required field.

    Example of charCodeAt() method :

    App.html

    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Click the button to display the unicode value of second character of the string "This is my home.".</p>
    
    <button onclick="showSecondCharUnicode()">Click</button>
    
    <p id="container"></p>
    
    <script>
    function showSecondCharUnicode() {
        var str = "This is my home.";
        var result = str.charCodeAt(1);
        document.getElementById("container").innerHTML = result;
    }
    </script>
    
    </body>
    </html>
    
    Output : 104
    

    In the above example the result 104 is an unicode value of the character h.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: