JavaScript tutorial-String object charAt | charCodeAt


JavaScript String object: charAt | charCodeAt

The charAt(index) method is used to get the character of the specified index in a string object. If you don't want to get the character but its code you can use the charCodeAt(index) method instead.

Example:
<html>
<head>
<title>Using concat() method</title>

<script language="JavaScript" type="text/javascript">

var str=new String("JavaScript is great.");
document.write("The character at index 0 is "+str.charAt(0)+" and its code is "+str.charCodeAt(0)+". ");

</script>

</head>
<body>
</body>
</html>

Another example is about using the charCodeAt() method to check whether the user entered a name starting with an upper case letter.

<html>
<head>
<title>Using chartCodeAt() method</title>

<script>
var nameUpper=prompt("Enter your name starting with upper case letter: ");
var str=new String(nameUpper);
if(str.charCodeAt(0)>"A".charCodeAt(0) && str.charCodeAt(0)>"Z".charCodeAt(0) )
document.write("Invalid!");
else
document.write("Welcome!");

</script>

</head>
<body>
</body>
</html>



Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.