Possible Duplicate:
Javascript: var functionName = function() {} vs function functionName() {}
I am curious to which is best pratice when creating a function in js
function x() {
...
}
OR
var x = function() {
...
}
Is there a difference or are they the exact same thing.
x(); // I work
function x() {
...
}
y(); // I fail
var y = function() {
...
}
The first is a function declaration. You can use functions before you've declared them.
The second is a assigning a function to a variable. This means you can assign to anything.
You can assing it to foo[0] or foo.bar.baz or foo.get("baz")[0]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With