+ 12
Function x (){} and var x = function(){}
What is the difference between Function x (){} and var x = function(){}
5 odpowiedzi
+ 14
JavaScript Functions 4 Ways
// Function Declaration
function square(x) {
return x * x;
}
// Function Expression
const square = function (x) {
return x * x;
}
// Arrow Function Expression
const square = (x) => {
return x * x;
}
// Concise Arrow Function Expression
const square = x => x * x;
+ 8
They has the same outcome, there is kind of no difference.
When var is used, you declare a variable of undefined type, and until you initialize it, it becomes a function; when function is used, you declare a function directly.
With function keyword your function is named, without function keyword your function is anonymous.
tests:
https://code.sololearn.com/WL98nGkr7pRj/?ref=app
Note:
The function name remained the same even after it has been copied.
And it can still be executed even after the original name is changed to other variable type.
There are also ES6 way:
https://www.sololearn.com/learn/JavaScript/2970/
+ 4
Thank you
+ 3
The later x is a variable reference to the function and the former x is the name of the function which is a reference to the actual function. It doesn't matter how you reference the function as long as the name points it
+ 2
function declaration does work with hoisting but not function expression point to remember