+ 5

Functions in JavaScript

What's the difference between: #Declaring a named function eg: function foo(){}; and #Declaring an anonymous function and assigning it to a variable eg: var foo = function(){};

15th Nov 2018, 1:25 AM
Umeozulu Tochukwu
Umeozulu Tochukwu - avatar
2 Respostas
+ 2
The former allows hoisting, which means you can use a function at the top of the code before it is declared somewhere in the bottom. The latter doesn't.
15th Nov 2018, 3:53 AM
Hoàng Nguyễn Văn
Hoàng Nguyễn Văn - avatar
+ 2
Both functions would be called as foo(). There isn't much of a difference. However, you could declare like this: const foo = function() {} to make foo() immutable, or unchangeable.
15th Nov 2018, 2:30 AM
yinhaodragon7
yinhaodragon7 - avatar