To get a character at a specific index in a string in JavaScript, you can use array-like indexing or the charAt()
method of the String object.
In this example,
str
with a value of "Hello World"
.charAt()
method of the string object str
.let str = "Hello World";
let charAtIndex3 = str[3]; // or str.charAt(3);
console.log(`Character at index 3: ${charAtIndex3}`);
Character at index 3: l
In this example,
str
with a value of "JavaScript"
.charAt()
method of the string object str
.let str = "JavaScript";
let charAtIndex1 = str[1]; // or str.charAt(1);
console.log(`Character at index 1: ${charAtIndex1}`);
Character at index 1: a
In this tutorial, we learned How to get Character at Specific Index in String in JavaScript language with well detailed examples.