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 and Number
- JS: String (class)
- JS: String Constructor
- JS: String.prototype