가장 기본적으로 사용하는 함수의 형태입니다. 기본적으로 함수는 함수 이름을 설정하고 함수 이름을 호출해야 실행이 됩니다.
function함수이름(){
//실행코드
}
함수이름(): //함수 호출
function func1() {
document.write("function가 실행되었습니다.");
}
func1();
//function가 실행되었습니다."
function func1() {
document.write("function1가 실행되었습니다.");
}
function func1() {
document.write("function2가 실행되었습니다.");
}
func1();
//function2가 실행되었습니다.
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script>
let color = ["white", "yellow", "aqua", "purple"];
let i = 0;
function changColor(){
i++;
if( i >= color.length ){
i = 0;
}
//if문을 쓰지않는다면 실행되지않음 if=초기화
let bodyTag = document.getElementById("theBody");
bodyTag.style.backgroundColor=color[i];
console.log("i : " + i);
console.log("color[i] : " + color[i]);
}
</script>
</head>
<body id="theBody">
<button onclick>배경색 바꾸기</button>
</body>
</html>