Function x (){} and var x = function(){} | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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
5 Answers
+ 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ć
Danijel Ivanović - avatar
+ 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
Gordon - avatar
+ 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
Dan Rhamba - avatar
+ 2
function declaration does work with hoisting but not function expression point to remember
27th Jan 2019, 1:40 AM
Danielov
Danielov - avatar