Home > Web Front-end > JS Tutorial > Introduction to the use of charAt() method in JavaScript_Basic knowledge

Introduction to the use of charAt() method in JavaScript_Basic knowledge

WBOY
Release: 2016-05-16 15:56:17
Original
1938 people have browsed it

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.
Grammar

string.charAt(index);

Copy after login

Here are the details of the parameters:

  • Index: An integer between 0 and 1 that is less than the length of the string.

Return value:

Returns the character from the specified index.
Example:

<html>
<head>
<title>JavaScript String charAt() Method</title>
</head>
<body>
<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)); 
</script>
</body>
</html>

Copy after login

This will produce the following results:

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 

Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template