JavaScript charAt() Method

JavaScript charAt() Method | The JavaScript charAt() method returns a character in a string at the specified index. The method takes in one argument which is the index or the position of the character to be returned. The index starts from the left and ends at the right-most character. The index of the first character is zero. The method also takes a negative number as an argument which means counting the index in the reverse order of the string. 

The return type of charAt() JavaScript method is of the String object. If the index provided in the argument is out of range the method returns an empty string. 

JavaScript String charAt() Method

Syntax:-
str.charAt(index)

Arguments:
The index in the string where the single character is to be retrieved is the only argument for this function. This index’s range, including its restrictions, is 0 to length – 1. The first character of the string is returned if no index is supplied because 0 is the function’s initial index.

Return value:
This function’s output is a single character in string format at the provided index. This function returns an empty string if the index is outside of the acceptable range.
Return Type: String

JavaScript charAt() Method Example

let string = "Hello World!";
let ch = string.charAt(6);
console.log(ch);
console.log(typeof ch);

Output:-

W
string

Using typeof operator we can check the type of any variable, and for the “ch” variable typeof gives a string. Hence charAt() method returns a string representation of the character.

let string = "Know Program";
console.log("string.charAt(0): " + string.charAt(0));
console.log("string.charAt(1): " + string.charAt(1));
console.log("string.charAt(2): " + string.charAt(2));
console.log("string.charAt(3): " + string.charAt(3));
console.log("string.charAt(4): " + string.charAt(4));
console.log("string.charAt(5): " + string.charAt(5));

Output:-

string.charAt(0): K
string.charAt(1): n
string.charAt(2): o
string.charAt(3): w
string.charAt(4):
string.charAt(5): P

A string’s characters are sorted from left to right. In a string named stringName, the first character’s index is 0 and the final character’s index is stringName.length – 1. JavaScript outputs an empty string if the index you provide falls outside of this range.

let string = "Know Program";
console.log("String length: " + string.length);
console.log("string.charAt(50): " + string.charAt(50));
console.log("string.charAt(99): " + string.charAt(99));

Output:-

String length: 12
string.charAt(50):
string.charAt(99):

let string = "Know Program";
console.log("string.charAt(-1): " + string.charAt(-1));
console.log("string.charAt(-40): " + string.charAt(-40));
console.log("string.charAt(-99): " + string.charAt(-99));

Output:-

string.charAt(-1):
string.charAt(-40):
string.charAt(-99):

We get the desired output, these line indicates there is no character that can be displayed, hence it returns a blank output.

The default value for JavaScript charAt() is 0 if no index is specified. The first character of the string is returned if no index is supplied.

String.charAt() JavaScript Example without parameter

let string = "Know Program";
console.log("First char: " + string.charAt());
let string1 = "Hello";
console.log("First char: " + string1.charAt());

Output:-

First char: K
First char: H

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *