JavaScript Code


How to get character at specific index from string?


Get character at specific index from string in JavaScript

Given a string value, get the character at specified index from the string.

Solution

To get the last character from the given string at specified index in JavaScript, use String charAt() method. charAt() method takes index as argument and returns the character at the index.

str.charAt(index)

Program

1. Get the character from the string value in str at index=5.

let str = 'helloworld';
let index = 5;
let ch = str.charAt(index);
console.log(ch);