Tutorials Point

  JavaScript Basics
  JavaScript Objects
  JavaScript Advanced
  JS Useful References
  JS Useful Resources
  Selected Reading

Copyright © 2014 by tutorialspoint

  Home     References     Discussion Forums     About TP  

Javascript String - charAt() Method

previous next AddThis Social Bookmark Button



This method returns the character from the specified index.

Characters in a string are indexed from left to right. The index of the first character is 0, and the index of the last character in a string called stringName is stringName.length - 1.



Here is the detail of parameters:

  • index: An integer between 0 and 1 less than the length of the string.

Return Value:

Returns the character from the specified index.


<title>JavaScript String charAt() Method</title>
<script type="text/javascript">
   var str = new String( "This is string" );
   document.writeln("str.charAt(0) is:" + str.charAt(0)); 
   document.writeln("<br />str.charAt(1) is:" + str.charAt(1)); 
   document.writeln("<br />str.charAt(2) is:" + str.charAt(2)); 
   document.writeln("<br />str.charAt(3) is:" + str.charAt(3)); 
   document.writeln("<br />str.charAt(4) is:" + str.charAt(4)); 
   document.writeln("<br />str.charAt(5) is:" + str.charAt(5)); 

This will produce following result:

str.charAt(0) is:T 
str.charAt(1) is:h 
str.charAt(2) is:i 
str.charAt(3) is:s 
str.charAt(4) is: 
str.charAt(5) is:i 

To understand it in better way you can Try it yourself.

previous next Printer Friendly