Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a function in JS [duplicate]

Tags:

javascript

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.

like image 654
austinbv Avatar asked Jun 20 '26 20:06

austinbv


1 Answers

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]

like image 140
Raynos Avatar answered Jun 22 '26 08:06

Raynos