How to Get Character of Specific Position using JavaScript ?

At 6/7/2023
In this article, we will see how to get the character of a specific position using JavaScript. There are three methods to get the character at a specific position using JavaScript. These are:
- Using String charAt() Method
- Using String substr() Method
- Using Square Bracket Notation
Using String charAt() Method: The charAt() method is used to get the character at a specific position in a string. This method takes the index as an argument and returns the character of the given index in the string.
Example:
Javascript
let str = "Welcome to Freesad";
let index = 11;
let ctr = str.charAt(index);
console.log("Character at " + index + "th position is: " + ctr);
Output:
Character at 11th position is: F
Using String substr() Method: The substr() method can also be used to get the character at a specific position in a string. This method returns the specified number of characters from a string, starting from the given index.
Example:
Javascript
let str = "Welcome to Freesad";
let index = 11;
let ctr = str.substr(index, 1);
console.log("Character at " + index + "th position is: " + ctr);
Output:
Character at 11th position is: F
Using Square Bracket Notation: The square bracket can also be used to get the character at a specific position in a string.
Example:
Javascript
let str = "Welcome to Freesad";
let index = 11;
let ctr = str[index];
console.log("Character at " + index + "th position is: " + ctr);
Output:
Character at 11th position is: F