Wednesday, July 3, 2024
HomeLanguagesJavascriptJavaScript Program to Access Individual Characters in a String

JavaScript Program to Access Individual Characters in a String

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 = neveropen
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

There are several methods that can be used to Check if a Number is Odd or Even, which are listed below:

  • Using loop with Square Bracket Notation
  • Using charAt() Method
  • Using slice() Method

We will explore all the above methods along with their basic implementation with the help of examples.

Approach 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 = "neveropen";
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
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

Approach 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 = "neveropen";
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
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

Approach 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
Character at index 8: p
Character at index 9: t

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments