for loop jaavscript code example
Example 1: javascript loop through array
var colors = ["red","blue","green"];
colors.forEach(function(color) {
console.log(color);
});
Example 2: javascript for loop[
for(i=0;i<=5;i++){
console.log(i);}
Example 3: for in loop javascript
function diagonalDifference(arr) {
let n = arr.length;
let pri,sec = 0;
pri = sec = 0;
for (let i= 0; i < n; i++) {
pri += arr[i][i];
sec += arr[i][n - i - 1];
}
if(pri==sec){
return (0);
}else {
if (pri>sec){
return (pri-sec);
}else{
return (sec-pri);
}
}
}