Difference between function myName() and var myName = function() in javascript? [duplicate] - javascript

This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 9 years ago.
Is there any difference between declaring a Javascript function like this:
function myName(...)
and like this:
var myName = function(...)
I don't believe so, but...

The first is a function declaration
You have given it a name.
It will be hoisted.
The second is a function expression
The way you wrote it, it's anonymous.
It will only be available after the line where it is defined.

Related

Javascript hoisting with variable and function [duplicate]

This question already has answers here:
Javascript - Precedence in hoisting
(2 answers)
Closed 7 months ago.
var a=2;
function a(){};
console.log(typeof(a));
I am wondering why the result of the above code is number. I know the var is hoisted but isn't it already at the first line of the code? so why the type of a is still a number instead of a function?
In this case function a(){} is actually hoisted, which you can see if you log a at various points:
console.log(a);
var a = 2;
console.log(a);
function a() {
console.log('hi');
}
console.log(a);

Explaining anonymous function [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Advanced JavaScript: Why is this function wrapped in parentheses? [duplicate]
(4 answers)
Closed 4 years ago.
Can anybody explain below code? All I know is it is anonymous function but what is (0) doing?
var output = (function(x) {
delete x;
return x;
})(0);
console.log(output);
Why output of above code comes zero. Can anybody explain?
That's because what you're doing is creating a function and then calling it immediately where x = 0. Your function is returning x, and thus 0.
As for what anonymous functions are, they are basically a function that gets stored in a variable. You call it from the variable instead of by name. So, for example:
var output = function (x) { return x;};
Can be called like this:
output(0);
As opposed to the normal way like this:
function myOutput(x) {
return x;
}
myOutput(0);

Is there a way to declare and instantly run function? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 6 years ago.
I've a code:
var myFn = function(){
//some code
}
myFn();
So I have to define a function, then run it in two rows.
Is there a way to define a function (with storage it in variable) and run it instantly, in one expression? Just a short way of this.
var myFn = function(){
console.log("test");
}()
myFn will be undefined because the function doesn't return anything

JavaScript Function within a function undefined [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
Can someone explain to me why the second function within the first function is undefined?
var a = 1
function abc () {
alert(a);
function xyz () {
alert(a);
}
}
https://jsfiddle.net/kp950/yLs73cth/
xyz is an inner function which is private to abc function.
You cannot call xyz unless you make it public
This is due to the scope at which you are trying to execute xyz() (globally). xyz() can only be run inside of abc() - where it is defined in the local scope.

JavaScript - Difference beetween object's method named once and name twice [duplicate]

This question already has answers here:
declare function name in object, Why? [duplicate]
(3 answers)
var functionName = function() {} vs function functionName() {}
(41 answers)
Closed 7 years ago.
First of all, sorry for bad title or description, I'm not completely good with English.
I want to know what is difference between this line of code:
var obj = {
...
func: function func() { ... },
...
}
and this:
var obj = {
...
func: function() { ... },
...
}
What is it special in naming a method twice? I saw both of these ways in a single JavaScript source code. Here you can take a look at source if it's needed.
Edit: Question is not about anonymous or non-anonymous function declaring, but about functions inside objects that are called methods.
One of the biggest (and most helpful) differences is that the non-anonymous function will provide the function name in stack traces.
The named version can be used recursively as Teemu points out.

Categories