What is the use of charAt() method in JavaScript?

JavascriptObject Oriented ProgrammingProgramming

charAt()

The charAt() method helps to find the character in a string at a particular index.It takes a single parameter(index) to execute character corresponding to the particular index.

syntax

string.charAt(index);

Example-1

In the following example when index 9 is passed as a parameter to charAt() method, output p is displayed.

 Live Demo

<html>
<body>
<p id="character"></p>
<script>
   var string = "TUTORIALSPOINT AND TUTORIX";
   var result = string.charAt(9)
   document.getElementById("character").innerHTML = result;
</script>
</body>
</html>

output

p

Example-2

In the following example when index 11 is passed as a parameter, charAt() method executes character a as output 

 Live Demo

<html>
<body>
<script>
   var string = "Tutorix is a best e-learning platform";
   var result = string.charAt(11)
   document.write(result);
</script>
</body>
</html>

output

a
raja
Published on 29-May-2019 18:14:51
Advertisements