1. 반복문(For statement)
1) for (시작조건; 종료조건; 변화조건) {}
for (let i = 0; i < 3; i += 1) {
console.log(i)
}
2) 활용 예제1
-HTML-
<h1>Hello world!</h1>
<ul></ul>
-JavaScript-
const ulEl = document.querySelector('.ul')
for (let i = 0; i < 3; i += 1) {
const li = document.createElement('li')
li.textContent = `list-${i + 1}`
ulEl.appendChild(li)
}
3) 활용 예제2
-HTML-
<h1>Hello world!</h1>
<ul></ul>
-JavaScript-
const ulEl = document.querySelector('.ul')
for (let i = 0; i < 10; i += 1) {
const li = document.createElement('li')
li.textContent = `list-${i + 1}`
if ((i + 1) % 2 === 0) {
li.addEventListener('click', function () {
console.log(li.textContent)
})
}
ulEl.appendChild(li)
}