JS: Iterate String
Enumerate Chars in String via For-Of Loop
Best way to go thru each char in string is using for-of Loop.
for (let x of "aπc") { console.log(x); } // a // π // c
Convert String to Array
Another way is to convert string to array, then use any array methods to go thru.
[..."aπc"].forEach( ((x) => { console.log(x); }) ); // prints // a // π // c
Using For-Loop (go thru 16-bits units)
If string contains emoji or rare Unicode character, for-loop behaves in unexpected way. (for why, see γsee JS: String Code Unitγ )
const str = "aπc"; for (let i = 0; i < str.length; i++) { console.log(str[i]); } // prints // a // οΏ½ // οΏ½ // c
JavaScript, String
- JS: String Overview
- JS: Quote String
- JS: Template String
- JS: String Escape Sequence
- JS: Unicode Escape Sequence
- JS: String Operations
- JS: Iterate String
- JS: String Code Unit
- JS: Count Chars in String π
- JS: Tagged Template String
- JS: Regex Functions
- JS: Convert String, Number
- JS: String Object
- JS: String Constructor
- JS: String.prototype