패스트캠퍼스

javaScript-함수(선언, 표현, 호이스팅)

용용it 2023. 5. 15. 14:31

함수의 기본 문법은 함수 선언문과 함수 표현식이 있다.

// 함수 선언문(Declaration)
function test() {}

// 함수 표현식(Expression)
const test() = function () {}

 

또한 호이스팅(Hoisting)은 함수 선언부가 유효 범위 최상단으로 끌어올려지는 현상을 말한다.

// 호이스팅(Hoisting)

function test1() {
	console.log('Test1')
}

test1() // Test1
test2() // test2

function test2() {
	console.log('test2')
}

 

하지만 함수 표현식으로 함수를 표현하면 호이스팅이 발생하지 않는다.

test()   // Uncaught ReferenceError: test is not defined

const test = function () {
	console.log(test)
}