Remember that your string has characters whose code-point is higher than U+FFFF, the behavior may not be as expected, see:
const string = 'Olá! ';
string
.split('')
.forEach((char) => console.log(char));
Learn more about why in this answer.
So, if you want to split strings that can contain these types of characters correctly, you can use more modern language features such as scattering operator (...):
const string = 'Olá! ';
[...string].forEach((char) => console.log(char));
Or you can use a loop for..of, using the iteration protocol built-in of strings, as well as the scattering notation above, in a more explicit way:
const string = 'Olá! ';
for (const char of string) {
console.log(char);
}
It is not necessary to use the
\nin this case because theconsole.logalready makes line break at the end.– JeanExtreme002
Really not necessary, thanks for the tip
– Gabriel José de Oliveira