while(조건){
반복해서 실행할 코드
}
// 조건(boolean) true -> flase가 될때까지 실행하게됨
var i = 0;
// 종료조건으로 i의 값이 10보다 작다면 true, 같거나 크다면 false가 된다.
while(i < 10){
// 반복이 실행될 때마다 coding everybody <br />이 출력된다.
document.write('coding everybody <br />');
// i의 값이 1씩 증가한다.
i++
}
codding everybody 가 9번 출력 (10은 조건에 충족되지 않으므로 9까지 출력된다. )
for(초기화; 반복조건; 반복이 될 때마다 실행되는 코드){
반복해서 실행될 코드
}
for(var i = 0; i < 10; i++){
document.write('coding everybody'+i+'<br />');
}
for(var i = 0; i < 10; i++){
if(i === 5) {
break;
}
document.write('coding everybody'+i+'<br />');
// 4까지 출력
}
for(var i = 0; i < 10; i++){
if(i === 5) {
continue;
}
document.write('coding everybody'+i+'<br />');
// 숫자 5를 제외한 부분 출
}
// 0부터 9까지 변수 i에 순차적으로 값을 할당
for(var i = 0; i < 10; i++){
// 0부터 9까지의 변수를 j의 값에 순차적으로 할당
for(var j = 0; j < 10; j++){
// i와 j의 값을 더한 후에 출력
// String은 숫자인 i와 j의 데이터 타입을 문자로 형태를 변환하는 명령이다.
document.write(String(i)+String(j)+'<br />');
// 00 ... 99까지 출력이됨.
}
}
안의 for문 j가 10번 반복한 후에 i가 실행이된다.
{
const arr8 = [100, 200, 300, 400, 500];
arr8.forEach(function(element){
document.write(element, "<br>");
});
document.write("<br>");
arr8.forEach(function(element, index, array){
document.write(element, "<br>"); //element : 배열 각각의 요소
document.write(index, "<br>"); // index : 배열 인덱스
document.write(array, "<br>"); // array: 배열 자체
});
document.write("<br>");
}
{
const arr10 = [100, 200, 300, 400, 500];
for (let i in arr10 ){
document.write( arr10[i], "<br>" );// 배열 각각의 요소 출력
}
document.write("<br>");
}
{
const arr11 = [100, 200, 300, 400, 500];
for (let i of arr11 ){
document.write( i, "<br>" ); // 배열 각각의 요소 출력
}
document.write("<br>");
}