Because of JavaScript's Declaration Process, Every Declarations will go to the Top of Code
without value
Without Value is very important. Let's look the example
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>함수의 동작원리 :: 선언적 함수 : 리턴타입+매개변수</title>
<script type="text/javascript">
var calc = function (x, y) {//function declaration
return x+y;
}
console.log(calc(5,3)); //8
var calc = function (x, y) {//function declaration
return x*y;
}
console.log(calc(5,3)); //15
</script>
</head>
<body>
</body>
</html>
first console.log will print 8, second will print 15.
this is the process of this code.
1. var calc will go to the top
2. x + y function will be a value of calc.
3. first console.log print 5+3
4. x * y function will be a value of calc.
5. second console.log print 5 * 3
But it is different between function and variable, lets see the example
<!DOCTYPE html>
<html>
<head>
<meta charset="EUC-KR">
<title>선언적 함수 정의 및 재정의 | 익명함수 정의 및 재정의</title>
<script type="text/javascript">
var func = function(){alert("func inner");}// 2 second create
function func(){alert("func declear");} //1 first create
func(); //func inner;
</script>
</head>
<body>
</body>
</html>
for this code, variable func and function func() will go to the top.
so this process is
1. func will be go to the top with first create function
2. func is overrided with second create.
3. func() executing