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:
// ONLINE-RUNNER:browser;
const text = 'Dirask';
console.log(text.charAt(0)); // D
// ^
// |
// character on 1st position (index=0)
Practical examples
1. Using String
charAt()
method
In this example, we use built-in String
charAt()
method to get n-th string character.
// ONLINE-RUNNER:browser;
const text = 'Dirask';
const index = 3;
const character = text.charAt(index);
console.log(character); // a
2. Using square brackets ([]
) notation
String characters can be accessed with []
like in array case.
// ONLINE-RUNNER:browser;
const text = 'Dirask';
const index = 3;
const character = text[index];
console.log(character); // a