JavaScript > String Methods

String CharCodeAt in JavaScript

How to return the Unicode of a particular character of a string in JavaScript?


CharCodeAt

Charcodeat is an string method {charCodeAt() method} in JavaScript which is used to unicode value of a specified character in a string. The unicode value of a character is in the form of number.

This method have one require parameter, i.e 'index', the first character index value is '0', second character index value is '1', like that and so on....

The Unicode values range from 0 to 1,114,111, but the charCodeAt() method returns only the values less than 65,536. 

If we want to retun the unicode value of characters from the start side just we need to give the index number to the charCodeAt() method {String.charCodeAt(2)}, the index counting of characters in a string starts from '0', that means first character is '0', second character is '1' like that and so on.....

In case if we want to return the unicode values of characters from end side we need to use the index 'String.length-1' for the first character, 'String.length-2' for the second character,and so on.....from the last.

In JavaScript, by using charCodeAt() we can return the Unicode of particular character of a string. For example if we want to return the Unicode of 8th character of the string we can use the below code.

To know more about Unicode values, click here.

Returning the unicode value of 8th character.

    <p>Click on the "Button" for returning the Unicode of 8th character of a string "SN ITFunda Service LLP".</p>
    <input type="button" value="Button" onclick="Function()" />
    <p id="myId"></p>

    <script>
        function Function() {
            var a = "SN ITFunda Service LLP";
            var n = a.charCodeAt(8)
            document.getElementById("myId").innerHTML = n;
        }
    </script>

In the above code snippet we have given ID as "myId" to the second <p> element and we used the string "SN ITFunda Service LLP" in the script code, and along with that we have given value 8 to the charCodeAt() method, that returns the Unicode of 8th character at the specified index in a string.

There is a button with value "Button" in the HTML page onclick of the button fires the function() in the script code and gives the 8th character Unicode number.

OUTPUT

Returning the unicode value of first character in a string.

<p>Click on the "Button" for returning the Unicode of first character of a string "SN ITFunda Service LLP".</p>
<input type="button" value="Button" onclick="Function()" />
<p id="myId"></p>

<script>
    function Function() {
        var a = "SN ITFunda Service LLP";
        var n = a.charCodeAt(0)
        document.getElementById("myId").innerHTML = n;
    }
</script>

First character unicode value

We need to return the Unicode value of a first character in a string, for that we are using charCodeAt(0) method. Onclick of the button returns the unicode value of the first character in a string.

OUTPUT

Returning the unicode value of last character in a string.

<p>Click on the "Button" for returning the Unicode of last character of a string "SN ITFunda Service LLP".</p>
<input type="button" value="Button" onclick="Function()" />
<p id="myId"></p>

<script>
    function Function() {
        var a = "SN ITFunda Service LLP";
        var n = a.charCodeAt(a.length-1)
        document.getElementById("myId").innerHTML = n;
    }
</script>

Last character unicode value

We need to return the Unicode value of a last character in a string, for that we are using charCodeAt(a.length-1) method. Onclick of the button returns the unicode value of the last character in a string.

OUTPUT

Returning the unicode values of different characters.

<p>Click on the "Button" for returning the Unicode of different characters of a string "SN ITFunda Service LLP".</p>
<input type="button" value="Button" onclick="Function()" />
<p id="myId"></p>

<script>
    function Function() {
        var a = "SN ITFunda Service LLP";

        var res = "2nd character unicode from Start:" + a.charCodeAt(1) + "</br>"
        + "2nd character unicode from End:" + a.charCodeAt(a.length - 2) + "</br>"
        + "3rd character unicode from Start:" + a.charCodeAt(2) + "</br>"
        + "3rd character unicode from End:" + a.charCodeAt(a.length - 3);
        document.getElementById("myId").innerHTML = res;
    }
</script>

Unicod evalues of different characters

We are returning the unicode values of different characters in a string by using charCodeAt() method.

OUTPUT

 Views: 8303 | Post Order: 68



Write for us






Hosting Recommendations