Skip to content
Related Articles
Open in App
Not now

Related Articles

JavaScript String charAt() Method

Improve Article
Save Article
  • Last Updated : 15 Dec, 2022
Improve Article
Save Article

JavaScript str.charAt() returns the character at the given index of the string index holds the array element position.

Syntax:

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. 

  • index: The range of this index is between 0 and length – 1. 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 Value: 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.

Below is an example of the String charAt() Method.

Example 1: 

JavaScript




<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);
        console.log(value);
        console.log(value1);
    }
    func();
</script>

Output:

J
S

Example 2: In this example, the function charAt() finds the character at index 9 and returns it. 

JavaScript




<script>
    function func() {
     
        // Original string
        var str = 'JavaScript is object oriented language';
     
        // Finding the character at given index
        var value = str.charAt(9);
        console.log(value);
    }
    func();
</script>

Output:

t

Example 3: 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. 

JavaScript




<script>
    function func() {
     
        // Original string
        var str = 'JavaScript is object oriented language';
     
        // Finding the character at given index
        var value = str.charAt(50);
        console.log(value);   
    }
    func();
</script>

Output:

 ""

We have a complete list of Javascript string methods, to check those please go through this Javascript String Complete reference article.

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

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!