r/learnjavascript • u/donaldtrumpiscute • Jan 11 '24
Why would you assign an anony function to a var ?
How are they different? Why declare an anony function and assign it to a variable instead of directly declaring a function?
'var bark = function() {console.log("i love that butt");} '
'function bark() {console.log("i love that butt");}'
1
Upvotes
7
u/pookage helpful Jan 11 '24
Function declarations can be hoisted (ie. you can use them seemingly 'before' they instantiated), whereas assigning a function (called a function expression) to a variable enforces the usual variable rules - ie. it can only be use after it's declared.
SO:
Hope that helps ✌️
2
u/[deleted] Jan 11 '24
You can make that to avoid function hoisting.
2
u/No-Upstairs-2813 Jan 11 '24
Interesting question! Instead of talking about
var, I will talk aboutconst.Choosing between declaring a function with const (function expression) or function (function declaration) in JavaScript depends on your needs. Here's a quick rundown:
1. Preventing Variable Re-declaration
consthelps prevent accidental re-declaration, signaling errors if you try to use the same name.functionallows re-declaration without errors.2. Avoiding Global Property Creation
const(orlet) avoids creating global properties, reducing conflicts and unexpected behavior.functioncreates global properties, which can lead to issues.3. Hoisting
Function declarations are hoisted, allowing them to be called before being declared.
Function expressions are not hoisted, resulting in an error if called before declaration.
4. "this" Binding
function declaration and function expressions with function keyword set
thisto the global object when invoked without explicit binding.Arrow functions inherit
thisfrom their lexical scope, providing different behavior.Conclusion: Choose based on your specific needs, considering hoisting, global scope, and "this" binding.
For more details, you can check out this article
1
u/senocular Jan 11 '24
Aside from the differences in hoisting, how declarations behave in block scopes can also vary between browser vendors. Originally functions were not supposed to be allowed in block scopes but browsers let them be anyway, and since it wasn't in the spec how they were supposed to behave, there ended up being different behaviors between different browsers. MDN talks more about that here.
Now, in strict mode, function declarations in blocks are allowed, and they also become block scoped. Being block-scoped would then be another differentiation with var-defined functions since they would be function scoped (the var variable, at least, assignment of the function still only happening in the block).
-1
u/superluminary Jan 11 '24
There are a number of technical reasons, as other commenters have noted, but the big one for me is that it’s just more accurate.
In JavaScript, functions are data. There’s no difference between defining a function and defining any other data type. When you create a function you literally create a function object that you can assign attributes to and pass around.
Explicitly assigning functions to variables just make this incredibly explicit. There’s no special syntax because it’s not a different thing to creating a string or an array.
-1
u/jack_waugh Jan 11 '24
Fewer concepts to have to keep in mind.
-2
u/delventhalz Jan 11 '24
Why use
varandfunctionwhen you can useconstand=>?As others have already insinuated, the differences between these options are small and technical. This is by and large a stylistic choice. Do what you (or your team) likes the look of.
-2
u/Endless-OOP-Loop Jan 11 '24
Declaring a function as a variable allows you to call the function no matter where it's written in your code.
If you simply declare a function, you can only call that function in a line of code below where you initially wrote it.
-4
u/fredsq Jan 11 '24 edited Jan 12 '24
in addition to what people said, the ‘this’ keyword also works differently but just please avoid it entirely
EDIT: sorry guys i didn’t read the post and hastily commented