📌
Javascript+jQuery
  • 자바스크립트 시작하기
  • 자바스크립트 기초 문법
  • 변수
  • 배열
  • 객체
  • 연산자
  • 조건문
    • if문
    • if~else문
    • 다중 if문
    • 중첩 if문
    • switch문
    • 삼항 연산자
  • 반복문
    • while문
    • do while문
    • for문
    • 중첩 for문
    • break문
    • continue문
  • 함수
    • 선언적 함수
    • 익명 함수
    • 매개변수가 있는 함수
    • arguments 함수
    • return값이 있는 함수
    • 재귀 함수
    • 콜백 함수
    • 내부함수(스코프)
    • 객체 생성자 함수
    • 프로토타입 함수
    • 화살표 함수
    • 함수정리
    • 템플릿 리터럴
    • 클래스
  • 내장객체
    • String 객체
      • split()
      • join()
    • Number 객체
    • Date 객체
    • Array 객체
    • Math 객체
    • 정규표현 객체
  • 브라우저 객체
    • window 객체
    • navigator 객체
    • screen 객체
    • history 객체
    • location 객체
  • 문자 객체
  • 이벤트
Powered by GitBook
On this page
  • 선언적 함수
  • 샘플1
  • 샘플2
  • 배경색 변경하기

Was this helpful?

  1. 함수

선언적 함수

가장 기보적으로 사용되느 함수의 형태입니다.

선언적 함수

가장 기본적으로 사용하는 함수의 형태입니다. 기본적으로 함수는 함수 이름을 설정하고 함수 이름을 호출해야 실행이 됩니다.

function함수이름(){ //실행코드 } 함수이름(): //함수 호출

샘플1

function func1() {
    document.write("function가 실행되었습니다.");
    }
    func1();
    
//function가 실행되었습니다."

샘플2

함수가 똑같으면 밑에 있는게 우선 순위가 높음

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>
Previous함수Next익명 함수

Last updated 4 years ago

Was this helpful?