JavaScript String charAt() Method - GeeksforGeeks

05 Oct, 2021
Below is the example of the String charAt() Method.
Example:<script>function func() {      // Original string    var str = 'JavaScript is object oriented language';      // Finding the character at given index     var value = str.charAt(0);     var value1 = str.charAt(4);     document.write(value);     document.write(value1);}func();</script>  
<script>function func() {      // Original string    var str = 'JavaScript is object oriented language';      // Finding the character at given index     var value = str.charAt(0);     var value1 = str.charAt(4);     document.write(value);     document.write(value1);}func();</script>  
Output:JS

JS

str.charAT() Returns character at given index of string.
character = str.charAt(index)

Arguments:The only argument to this function is the index in the string from where the single character is to be extracted. The range of this index is between 0 and length – 1, including the limits. If no index is specified then the first character of the string is returned as 0 is the default index used for this function.
Return valueThis function returns a single character located at the index specified as the argument to the function. If the index is out of range, then this function returns an empty string.
Examples for the above function are provided below:
Example 1:
var str = 'JavaScript is object oriented language';
print(str.charAt(9));

Output:
t

In this example the function charAt() finds the character at index 9 and returns it.
Example 2:
var str = 'JavaScript is object oriented language';
print(str.charAt(50));

Output:


In this example the function charAt() finds the character at index 50. Since the index is out of bounds for the given string therefore the function returns “” an empty string.
Codes for the above function are provided below:
Program 1:
// JavaScript to illustrate charAt() function<script>function func() {      // Original string    var str = 'JavaScript is object oriented language';      // Finding the character at given index     var value = str.charAt(9);    document.write(value);  }func();</script>  
Output:
t

Program 2:
<script>// JavaScript to illustrate charAt() functionfunction func() {      // Original string    var str = 'JavaScript is object oriented language';      // Finding the character at given index     var value = str.charAt(50);    document.write(value);    }func();</script>  
Output:


Supported Browser:
Chrome 1 and above
Edge 12 and above
Firefox 1 and above
Internet Explorer 3 and above
Opera 3 and above
Safari 1
Akanksha_Rai
ysachin2314
javascript-string
JavaScript
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Difference between var, let and const keywords in JavaScript
Convert a string to an integer in JavaScript
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
How to Open URL in New Tab using JavaScript ?
Top 10 Front End Developer Skills That You Need in 2022
Installation of Node.js on Linux
Top 10 Projects For Beginners To Practice HTML and CSS Skills
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?