Open In App

JavaScript Program to Access Individual Characters in a String

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to learn about accessing individual characters from the given string. Accessing individual characters in a string means retrieving a specific character from the string using its index position (zero-based) or iterating through each character one by one.

Example:

Input : str = GeeksforGeeks
Output :
Character at index 0: G
Character at index 1: e
Character at index 2: e
Character at index 3: k
Character at index 4: s
Character at index 5: f
Character at index 6: o
Character at index 7: r
Character at index 8: G
Character at index 9: e
Character at index 10: e
Character at index 11: k
Character at index 12: s

Examples of Accessing Individual Characters in a String

1.Using loop with Square Bracket Notation

In this approach, Initialize a string `str` and then use a `for` loop to iterate over each character in the string. The loop starts at the first character (index 0) and continues until it reaches the end of the string (str.length – 1). During each iteration of the loop, the program uses the square bracket notation to access the character at the current index and then logs it to the console.

Syntax:

function access(str) {
for (let i = 0; i < str.length; i++) {
const char = str[i];
console.log(`Character at index ${i}: ${char}`);
}
};

Example: In this example, we are using the above-explained approach.

Javascript
function access(str) {
    for (let i = 0; i < str.length; i++) {
        const char = str[i];
        console.log(`Character at index ${i}: ${char}`);
    }
}

const str = "GeeksforGeeks";
access(str);

Output
Character at index 0: G
Character at index 1: e
Character at index 2: e
Character at index 3: k
Character at index 4: s
Character at index 5: f
Character at index 6: o
Character at index 7: r
Characte...

2.Using charAt() Method

The charAt() method is used to access the individual characters of a string. This method takes the index as an argument and returns the character of the given index in the string.

Syntax:

character = str.charAt(index)

Example: In this example, we are using the above-mentioned method.

Javascript
function access(str) {
    for (let i = 0; i < str.length; i++) {
        const char = str.charAt(i);
        console.log(`Character at index ${i}: ${char}`);
    }
}

const str = "GeeksforGeeks";
access(str);

Output
Character at index 0: G
Character at index 1: e
Character at index 2: e
Character at index 3: k
Character at index 4: s
Character at index 5: f
Character at index 6: o
Character at index 7: r
Characte...

3.Using slice() Method

The string.slice() is an inbuilt method in JavaScript that is used to return a part or slice of the given input string. Using the slice method we can easily access individual characters from the String.

Syntax:

arr.slice(begin, end)

Example: In this example, we are using the above-explained approach.

Javascript
function access(str) {
    for (let i = 0; i < str.length; i++) {
        const character = str.slice(i, i + 1);
        console.log(`Character at index ${i}: ${character}`);
    }
}

const str = "JavaScript";
access(str);

Output
Character at index 0: J
Character at index 1: a
Character at index 2: v
Character at index 3: a
Character at index 4: S
Character at index 5: c
Character at index 6: r
Character at index 7: i
Characte...

4.Using split and forEach Method

This approach use the split(”) method to split the string into an array of individual characters, then we use the forEach method to iterate over each character in the chars array.

Syntax:

array=string.split('')
array.forEach(currentValue, index) {
// Function body
});

Example: this example implements the the above-explained approach.

Javascript
const str = "GeeksForGeeks";
const chars = str.split('');
chars.forEach((char, index) => {
    console.log(`Character at index ${index}: ${char}`);
});

Output
Character at index 0: G
Character at index 1: e
Character at index 2: e
Character at index 3: k
Character at index 4: s
Character at index 5: F
Character at index 6: o
Character at index 7: r
Characte...


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads