JS: String.prototype.at (get char at index) β
(new in ECMAScript 2022)
str.at(index)-
get the character at index. Return a string of single char.
- Index can be negative, means count from right.
- Out of bound index return undefined.
π warning: if string contains emoji (π¦) or rarely used unicode character, result of string methods may not be what you expect. See JS: String Index and Code Unit
console.log("abc".at(0)); // a console.log("abc".at(1)); // b console.log("abc".at(-1)); // c // out of bound index return undefined console.log("abcd".at(9) === undefined); /* does not work if string contains unicode character outside basic multilingual plane (e.g. emoji) */ console.log("π¦".at(0)); // οΏ½ /* π¦ Name: BUTTERFLY ID 129419 HEXD 1F98B UTF8 F0 9F A6 8B UTF16 D83E DD8B */
String.prototype .charAt vs .at
- βcharAtβ is older.
- βatβ version is newer. It supports negative index