STEP 4 - Creating Elements

Ahn·2021년 8월 3일
0

Prep Guide

목록 보기
3/4

ex1)

const something = document.createElement("p");

for (let i = 0; i < 5; i++) {
  something.textContent = i;
  document.body.appendChild(something);
}

// 4

ex2)

for (let i = 0; i < 5; i++) {
  const something = document.createElement("p");
  something.textContent = i;
  document.body.appendChild(something);
}
/*
  0
  1
  2
  3
  4
*/

차이점

ex1)
something이 for문 밖에서 선언 되었기 때문에 p태그 한번만 생성됨.. for문 마지막 숫자인 4만 출력

ex2)
something이 for문 안에 선언 되어서 p태그 5번 생성됨..
0~4 출력됨

profile
개발시작

0개의 댓글