string loop in javascript code example
Example 1: javascript loop through string
for (var i = 0; i < str.length; i++) {
console.log(str.charAt(i));
}
Example 2: for of loop javascript
const array1 = ['a', 'b', 'c'];
for (const element of array1) {
console.log(element);
}
Example 3: javascript loop through array
Array.prototype.myEach = function(callback) {
for (let i = 0 ; i < this.length ; i ++) {
callback(this[i]);
}
}
let array = ['Item 1', 'Item 2', 'Item 3', 'Item 4'];
array.myEach(function (element) {
console.log(element);
});