反復処理(繰り返し)
/**
* 反復処理(繰り返し)
*/
// while -----------------------------------------------
function whi(){
var x = 8;
while(x < 10) {
console.log('while xの値は' + x);
x++;
}
}
whi();
// do...while -----------------------------------------------
function doWhi(){
var x = 8;
do {
console.log('do...while xの値は' + x);
x++;
} while(x < 10);
}
doWhi()
// for -----------------------------------------------
function fo(){
for (var x = 0; x < 5; x++) {
console.log('for ' + 'xの値は' + x);
}
}
fo();
// for...in -----------------------------------------------
function forI(){
var data = { A: 30, B: 10, C: 20 };
for (var key in data) {
console.log('for...in ' + key + '=' + data[key]);
}
}
forI();
// for...of (ES2015) -----------------------------------------------
function forO() {
var data = [ 'apple', 'orange', 'banana' ];
Array.prototype.hoge = function() {}
for (var value of data) {
console.log(value);
}
}
forO();
See the Pen js: 反復処理 by nwstcode (@nwst) on CodePen.