JavaScript Code

JavaScript – Access Character in String using Square Brackets


Access Character in String using []

str[n]

str[n] returns the character from the string str present at the index n.

Examples

1. character in string at index

let name = 'apple';
let output = name[3];
console.log(output);

2. n is out of the range for given string length. str[n] returns undefined.

let name = 'apple';
let output = name[15];
console.log(output);