if (조건) {실행
} else if (조건) {실행
} else {실행}
// getRandom.js
export default function random(){
return Math.floor(Math.random() * 10) // .floor는 내림 메소드(버림)
}
// main.js
import random from './getRandom'
const j = random()
if(j === 0){
console.log('j is 0')
} else if (j === 2){
console.log ('j is 2')
} else if (j === 3){
console.log ('j is 3')
}else {
console.log('rest...')
}
// mains.js
import random from './getRandom'
const j = random()
switch (j) {
case 0:
console.log('j is 0')
break
case 2:
console.log('j is 2')
break
case 4:
console.log('j is 4')
break
default :
console.log('rest...')
}
for (시작조건; 종료조건; 변화조건) { }
<!-- index.html-->
<head>
<script defer src ="./main.js"></script>
</head>
<body>
<ul></ul>
</body>
// main.js
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)
}
/* 결과
list - 1
list - 2
list - 3
list - 4
list - 5
list - 6
list - 7
list - 8
list - 9
list - 10 */