[javascript] for, for in, for of


for

var data = [1,2,undefined,NaN,null,""];
Array.prototype.getIndex = function() {};

for(var i = 0; i < data.length; i++) {
	console.log(data[i]);
}

結果



for in


位相の付加値も出力できます.
var data = [1,2,undefined,NaN,null,""];
Array.prototype.getIndex = function() {};

for (let idx in data) {
	console.log(data[idx]);
}

結果



for of

var data = [1,2,undefined,NaN,null,""];
Array.prototype.getIndex = function() {};

for (let value of data) {
	console.log(value);
}

結果



stringでも使えます.
var str = "hello world"
for (let value of str) {
	console.log(value);
}

結果