JavaScript String charAt (with Example)

In this tutorial, we are going to learn about the javascript string charAt() method with the help of multiple examples.

The javascript string charAt() method is used to return the character at the specified index. It also has some other use cases like if we want to iterate over every character of a string then we can do so simply using the string charAt() method.

Example

let str = "Welcome to Codingclaw";

// returning character at the 3rd index.
let char = str.charAt(3);

console.log(`Character at 3rd index is : ${char}`);
//Output: Character at 3rd index is : c

Also Read :

Javascript substring() method
Javascript Split() String Method
Javascript String replace() Method

Javascript charAt() Syntax

Here, str is a string.

Javascript charAt() Parameters

index : It’s an integer between 0 to str.length - 1. If no index is provided, 0 will be assumed as the default, which returns the first character of the string.

Note : str.length will return the length of the string.

Javascript charAt() Return Value

It returns the character at the specified index. If the index you provided is out of range then charAt() will return the empty string.

Example 1: Using Javascript charAt() with Integer Index

let str = "Welcome to Codingclaw";

// returning character at the 5 index.
let char = str.charAt(5);

console.log(`Character at the 5 index is : ${char}`);
//Output: Character at 5 index is : m

Output

Character at the 5 index is: m

In the above program, str.charAt(5) is returning the character at index 5 of the given string.

Example 2: Using Javascript charAt() without Passing Parameter

let str = "Welcome to Codingclaw";

// using without parameter.
let char = str.charAt();
// As we have not provided any index
// by default it will return a character at 0 index
console.log(`Character at 0 index is : ${char}`);
//Output: Character at 0  index is : W

Output

Character at 0  index is : W

In the above program, we haven’t provided any index so by default 0 will be assumed. So the first character of the string will be returned.

Example 3: Using Javascript charAt() with Out of Range Index Value

let str = "Welcome to Codingclaw";

//Providing out of range index
let char = str.charAt(100);
// As we have providing out of range index
// empty string will be returned
console.log(`Character at 100 index is : ${char}`);
//Output: Character at 100 index is : 

Output

Character at 100 index is :

In the above, we have provided an out-of-range value. In this condition charAt() will return an empty string.

Example 4: Using Javascript charAt() with Non-Integer Index Value

let str = "Welcome to Codingclaw";

//returning character at 2.4 index
let char1 = str.charAt(2.4);
console.log(`Character at 2.4 index is : ${char1}`);
//Output: Character at 2.4 index is : l

//returning character at 2.9 index
let char2 = str.charAt(2.9);
console.log(`Character at 2.9 index is : ${char2}`);
//Output: Character at 2.9 index is : l

//returning character at 2 index
let char3 = str.charAt(2);
console.log(`Character at 2 index is : ${char3}`);
//Output: Character at 2 index is : l

Output

Character at 2.4 index is : l
Character at 2.9 index is : l
Character at 2 index is : l

In the above program, we have used non-integer values as index, which are converted to their nearest integer. So both the values 2.4 and 2.9 are rounded to the nearest integer which is 2. That’s why we are getting the same character output as str.charAt(2).

Leave a Comment

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