EN
JavaScript - get nth string character
3 points
In this article, we would like to show you how to get n-th string character in JavaScript.
Quick solution:
xxxxxxxxxx
1
const text = 'Dirask';
2
3
console.log(text.charAt(0)); // D
4
// ^
5
// |
6
// character on 1st position (index=0)
In this example, we use built-in String
charAt()
method to get n-th string character.
xxxxxxxxxx
1
const text = 'Dirask';
2
3
const index = 3;
4
const character = text.charAt(index);
5
6
console.log(character); // a
String characters can be accessed with []
like in array case.
xxxxxxxxxx
1
const text = 'Dirask';
2
3
const index = 3;
4
const character = text[index];
5
6
console.log(character); // a