javascript

JavaScript/ Hoisting, Function and Variable

MyaZ 2020. 2. 14. 16:37

함수 선언식 - Function Declarations
함수 표현식 - Function Expressions

 

// <function>
//this is function declaration. 
//function hoisting is only work for function declaration.
calculateAge(1965);//<-hoisting: we can call the function before even we declaire the function.

function calculateAge(year){
    console.log(2016-year);
}

//this is function expretion
//retirement(1956);<- you can not bring out the result here through function expretions.
var retirement = function(year){
    console.log(65-(2016-year));
}

retirement(1956);

 

// <variable>
console.log(age);// printed: undefined // variable that doesn't have values yet It always has data type 'undefined'.
var age= 23;//글로벌변수

function foo(){
   // console.log(age); //undefined
    var age =65;//지역변수
    console.log(age);
}

foo();//65 
console.log(age);//23
// each of them get their own variable obj. so printed results are diffrent.