Function x (){} and var x = function(){} | Sololearn: Learn to code for FREE!
+ 12

# Function x (){} and var x = function(){}

What is the difference between Function x (){} and var x = function(){}

25th Jan 2019, 3:50 PM
CoderJ
+ 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;
25th Jan 2019, 4:42 PM
Danijel IvanoviÄ
+ 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/
25th Jan 2019, 4:14 PM
Gordon
+ 4
Thank you
25th Jan 2019, 4:17 PM
CoderJ
+ 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
26th Jan 2019, 4:32 AM
Dan Rhamba
+ 2
function declaration does work with hoisting but not function expression point to remember
27th Jan 2019, 1:40 AM
Danielov
Hot today
PHP