Languages
[Edit]
EN

JavaScript - get nth string character

3 points
Created by:
Lilly-Grace-Greig
481

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

Alternative titles

  1. JavaScript - get n-th string character
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join