JQuery Functions Declarations

What is the difference between:

function removeLightbox() {...}


removeLightbox = function(){...}

Second one is more flexible, allowing you to declare function depending on conditions, like:

 var removeLightbox = function() { return 'f1'; };
 var removeLightbox = function() { return 'f2'; };

then second one can be used inside objects, like $.removeLightbox = function() {}; which can’t be done with first method.

With second method you can’t use function before its declaration, but with first method you can.

That’s all I can think of. I’m sure there are many other differences.

Thanks, that helps for now

The first one declares a function using the function statement. The second declares a function using a function operator.

The main difference between them is that functions created using the function operator do not exist until execution reaches that code. Function statements are hoisted to the top of their context. That means that a function statement can be made at the bottom of the code, but it can still be used from places above the function statement.

So while the function statement has hoisting as its advantage, it’s a hidden advantage that most people aren’t explicitly aware of, even though they implicitly make use of it.

The function operator allows you to create functions just when they’re needed.