If the order of alerts matters, use this:
for (var i = 0; i < str.length; i++) {
alert(str.charAt(i));
}
Or this: (see also this answer)
for (var i = 0; i < str.length; i++) {
alert(str[i]);
}
If the order of alerts doesn’t matter, use this:
var i = str.length;
while (i--) {
alert(str.charAt(i));
}
Or this: (see also this answer)
var i = str.length;
while (i--) {
alert(str[i]);
}
var str="This is my string";
function matters() {
for (var i = 0; i < str.length; i++) {
alert(str.charAt(i));
}
}
function dontmatter() {
var i = str.length;
while (i--) {
alert(str.charAt(i));
}
}
<p>If the order of alerts matters, use <a href="#" onclick="matters()">this</a>.</p>
<p>If the order of alerts doesn't matter, use <a href="#" onclick="dontmatter()">this</a>.</p>