• Register
Welcome to Kodlogs, programming questions and answer website.
0 votes
33 views
Type String and codeCharAt in JavaScript: Codingame: ASCII art issues:

What is charCodeAt? What does charAt do in JavaScript?
by (140 points)  
reshown by

1 Answer

0 votes

charCodeAt:

charCodeAt could be used to find Unicode of usually first character in a string values.

var test = "First Program";

var test2 = str.charCodeAt(0);

<p> "First Program".</p>

<button onclick="FunctionTest()">Test</button>

<p id="test"></p>

<script>

function FunctionTest() {

  var str = "First Program";

  var n = str.charCodeAt(0);

  document.getElementById("test").innerHTML = n;

}

</script>

</body>

charCodeAt Syntax

string.charCodeAt(index)

Parameter Values

Parameter Description

index         Mandatory. A number will mean character index 

Another example of charCodeAt

<!DOCTYPE html>

<html>

<body>

<p>"Second Program".</p>

<button onclick="secFunc()">Click to test charCodeAt</button>

<p id="test"></p>

<script>

function secFunc() {

  var str = "Second Program";

  var n = str.charCodeAt(str.length-1);

  document.getElementById("test").innerHTML = n;

}

</script>

</body>

</html>

Browser compatibility of charCodeAt:

chrome

IE

Opera

Safari

Android Webview

Node.js

Further Readings:

ECMAScript First Edition (ECMA-262)
ECMAScript 5.1 (ECMA-262)
The definition of 'String.prototype.charCodeAt'  specification.
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'String.prototype.charCodeAt' in that specification.
ECMAScript Latest Draft (ECMA-262)
The definition of 'String.prototype.charCodeAt' in that specification.
Draft
by (9.7k points)  
edited by
...