728x90
데이터 제어하기
01. if문
{
if(조건식){
document.write("실행되었습니다.(true)");
} else {
document.write("실행되었습니다.(false)");
}
}
결과 값: 실행되었습니다(false)
02. if문 생략
{
const num = 100;
if(num) document.write("실행되었습니다.(true)");
else document.write("실행되었습니다.(false)");
}
결과 값: 실행되었습니다(false)
03. 삼항 연산자
{
const num = 100;
if(num == 100){
document.write("true");
}else {
document.write("false");
}
(num == 100) ? document.write("true") : document.write("false");
}
결과 값: 실행되었습니다(false)
04. 다중 if
{
const num = 100;
if(num == 90){
document.write("실행되었습니다.(num == 90)");
} else if(num == 100){
document.write("실행되었습니다.(num == 100)");
} else if((num == 110)){
document.write("실행되었습니다.(num == 110)");
}else if((num == 120))
document.write("실행되었습니다.(num == 120)");
else {
document.write("실행되었습니다.)");
}
}
결과 값: 실행되었습니다(num==100)
05. 중첩 if문
{
const num = 100;
if(num == 100) {
document.write("실행되었습니다(1)");
if(num == 100){
document.write("실행되었습니다(2)");
if(num == 100){
document.write("실행되었습니다(3)");
}
}
} else {
document.write("실행되었습니다(4)");
}
결과 값: 실행되었습니다.(1)실행되었습니다.(2)실행되었습니다.(3)
06. 데이터 제어하기 : switch문
const num = 100;
switch(num){
case 90:
document.write("실행90");
break;
case 80:
document.write("실행80");
break;
case 70:
document.write("실행70");
break;
case 60:
document.write("실행60");
break;
case 50:
document.write("실행50");
break;
default:
document.write("0");
}
결과 값: 0
07. 데이터 제어하기 : while문
for(let i=0; i<5; i++){
document.write(i);
}
let num = 0;
while(num<5){
document.write(num);
num++;
}
결과 값: 0123401234
08. 데이터 제어하기 : do while문
let num = 0;
do {
document.write(num);
num++;
} while (num<5);
결과 값: 01234
09. for문
{
const arr = [1,2,3,4,5,6,7,8,9];
for(let i=1; i<arr.length; i++){
if(i % 2 == 0){
document.write("<span style='color:red'>"+i+"");
}else {
document.write(`<span style='color:blue'>${i}`);
}
}
결과 값: 12345678
10. 데이터 제어하기 : 중첩 for문
{
let table = "<table border=1>";
let count = 1;
for(let i=1; i<=5; i++){
table += "<tr>"
for(let j=1; j<=5; j++){
if(count % 2 == 0){
table += "<td style='color:red'>" + count + "</td>"
} else {
table += "<td style='color:blue'>" + count + "</td>"
}
count++
}
table += "</tr>"
}
table += "</table>";
document.write(table);
}
11. 데이터 제어하기 : break문
for(let i=1; i<20; i++){
if(i == 10){
break;
}
document.write(i);
}
결과 값: 123456789