for…in 루프
"for...in" 루프는 개체의 속성을 반복하는 데 사용됩니다.
다음은 구문입니다 -
구문
for (variablename in object) {
statement or block to execute
} 다음 예제를 실행하여 'for-in' 루프를 구현할 수 있습니다. 웹 브라우저의 Navigator 개체를 인쇄합니다.
예시
라이브 데모
<html>
<body>
<script>
var aProperty;
document.write("Navigator Object Properties<br /> ");
for (aProperty in navigator) {
document.write(aProperty);
document.write("<br />");
}
document.write ("Exiting from the loop!");
</script>
</body>
</html> for...of 루프
"for…of" 루프는 Map, Array, 인수 등을 포함하는 반복 가능한 객체를 반복하는 데 사용됩니다.
구문
구문은 다음과 같습니다 -
for (variablename of iterable){
statement or block to execute
} 예시
다음은 for…of 루프를 사용한 반복을 보여주는 예입니다.
라이브 데모
<!DOCTYPE html>
<html>
<body>
<script>
let itObj= [20, 30, 40, 50];
for (let res of itObj) {
res += 1;
document.write("<br>"+res);
}
</script>
</body>
</html> 출력
21 31 41 51