JavaScript Code

How to get character at specific index from string?


Problem Statement

given a string, 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

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);