var functionName = function() {} vs function functionName() {}
The difference is that functionOne
is a function expression and so only defined when that line is reached, whereas functionTwo
is a function declaration and is defined as soon as its surrounding function or script is executed (due to hoisting).
For example, a function expression:
// TypeError: functionOne is not a functionfunctionOne();
var functionOne = function() { console.log("Hello!");};
var functionName = function() {} vs function functionName() {}
The difference is that functionOne
is a function expression and so only defined when that line is reached, whereas functionTwo
is a function declaration and is defined as soon as its surrounding function or script is executed (due to hoisting).
For example, a function expression:
// TypeError: functionOne is not a functionfunctionOne();
var functionOne = function() { console.log("Hello!");};
what's the difference between var function and function in javascript?
here is a best article that may help you.
refer http://www.dustindiaz.com/javascript-function-declaration-ambiguity/
function aPrint() {}
Declares a function (but does not execute it).
It will usually have some code between the curly brackets.var a = aPrint()
Declares a variable, invokes a function (aPrint) and sets the value of aPrint to the return of the function.var a= new aPrint()
Creates a new instance of an object based on the aPrint function. So the variable is now an Object, not just a string or a number.
Objects can contain indexed strings, numbers and even functions, and you can add more stuff to them, they're pretty awesome. The whole concept of Object Oriented Programming (OOP) is based on this.
Javascript: performance of var functionName = function() {} vs function functionName() {}
Much more important than performance differences are the semantic differences between those two.
- A function declared with a function declaration statement (second sample) has a name that will show up in stack traces etc.
- Function declaration statements are "hoisted" to the top of their blocks and interpreted as if they actually appeared there, before any other statements in the function run.
The performance differences are probably pretty tiny, if even detectable, at least in modern runtime environments.
Is there any difference between var name = function() {} & function name() {} in Javascript?
They are mostly the same.
utilFunction1
will only be available after it has been declared. utilFunction2
is hoisted to the top of the function, so can be used before it is defined.
function someGlobalFunction() {
utilFunction1(); // Error: untilFunction1 is undefined :(
utilFunction2(); // Works
var utilFunction1 = function() {
}
function utilFunction2 () {
}
}
Unless they are trapped in a closure, both will cease to exist when someGlobalFunction
returns.
I prefer to use the method used to declare utilFunction2
, but it's up to you.
Declarations of the form utilFunction2
(which are called Function Declarations) have the benefit of being named (i.e. showing up as utilFunction2
) in your-favourite-debuggerTM, where as utilFunction1
(called Function Expressions) would just show up as an anonymous function.
For completeness, you also have the form;
var utilFunction3 = function utilFunction4() {
// blah
};
... which is called a named function expression, which has weird properties (and bugs (in older versions of IE)) of its own.
var functionName = function() {} vs function functionName() {}
The difference is that functionOne
is a function expression and so only defined when that line is reached, whereas functionTwo
is a function declaration and is defined as soon as its surrounding function or script is executed (due to hoisting).
For example, a function expression:
// TypeError: functionOne is not a functionfunctionOne();
var functionOne = function() { console.log("Hello!");};
Related Topics
Changing a CSS Rule-Set from JavaScript
How to Make a Div Stick to the Top of the Screen Once It's Been Scrolled To
How to Change the Background Color With JavaScript
How to Simulate a Mouseover in Pure JavaScript That Activates the CSS ":Hover"
Change an Image With Onclick()
How to Get Value of a Div Using JavaScript
Html5 Canvas Drawimage Ratio Bug Ios
Jquery: Why Use Document.Ready If External Js At Bottom of Page
How to Create a Sticky Navigation Bar That Becomes Fixed to the Top After Scrolling
How to Use Jquery to Select a Dropdown Option
Instead of Using Prefixes I Want to Ask Site Visitors to Upgrade Their Browser
How to Disable a Browser or Element Scrollbar, But Still Allow Scrolling With Wheel or Arrow Keys
Replace HTML Page With Contents Retrieved Via Ajax
How to Disable Scroll Without Hiding It