The charAt() & charCodeAt() Methods

There are 2 safe methods for extracting string characters and these are the charAt(position) and the charCodeAt(position) methods. The charAt() method returns the character at a specified index (position) in a string:
   var str = "HELLO WORLD";
   str.charAt(0); 
This will return H.

The charCodeAt() method returns the unicode of the character at a specified index in a string:
   var str = "HELLO WORLD";
   str.charCodeAt(1); 
This will return 69 (E is the HTML character code for E).

HTML file: Displayed by browser:
<body>
<p>The charAt() method returns the character at a given position in a string:</p>
<p id="demo"></p>
<hr />
<p>The charCodeAt() method returns the unicode of the character at a given position in a string:</p>
<p id="demo1"></p>
<script>
    var str = "HELLO WORLD";
    document.getElementById("demo").innerHTML = str.charAt(0);
    document.getElementById("demo1").innerHTML = str.charCodeAt(1)
</script>
</body>

The charAt() method returns the character at a given position in a string:


The charCodeAt() method returns the unicode of the character at a given position in a string:


BackTable of ContentsNext