JavaScript : function declarations vs function expressions

スポンサーリンク

Basically, there are two ways to define a JavaScript function.

function white(){};

var declare = function white(){};

The first one is function declaration, and the later one is function expression which put a function into a defined variable.

The difference between the two is below.

hoisted();  //output the console log

notHoisted();  //output undefined

function hoisted(){

console.log('this function is hoisted!');

}

var notHoisted = function() {

console.log('this function is not hoisted!');

}

In Javascript,  variables and functions are "hoisted" and they are actually available before they are declared, however,

function definition hoisting only occurs for function declarations, not function expressions.

Reference :

404 Not Found
Named function expressions demystified
An article about named function expressions in Javascript

コメント

タイトルとURLをコピーしました