Languages
[Edit]
EN

JavaScript - get nth string character

0 points
Created by:
Lilly-Grace-Greig
211

In this article, we would like to show you how to get nth string character in JavaScript.

Quick solution:

// ONLINE-RUNNER:browser;

const text = "Dirask";
const n = 0;
const result = text.charAt(n);

console.log(result);  // D

 

1. Practical example using charAt() method

In this example, we use built-in charAt() method to get nth string character.

// ONLINE-RUNNER:browser;

const text = "Dirask";
const n = 3;
const result = text.charAt(n);

console.log(result);  // a

Output:

a

2. Using square brackets notation [] 

In this example, we use the square brackets notation ([]) to get nth string character.

// ONLINE-RUNNER:browser;

const text = "Dirask";
const n = 3;
const result = text[n];

console.log(result);  // a

Output:

a
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