2

var a = function b() {
};
console.log(typeof b); //gives undefined
console.log(typeof a); //gives function

Why the difference in the two outputs?

I understand the difference between function expression and function statement, but not able to understand the output above.

From what I know, javascript makes var a point to the memory allocated to named function b here. In such a case typeof b should also return function but it returns undefined

Any explanations?

ibrahim mahrir
31.8k5 gold badges50 silver badges78 bronze badges
asked Feb 25, 2017 at 12:49
1
  • See kangax.github.io/nfe. Commented Feb 25, 2017 at 13:10

3 Answers 3

6

Because the name of a named function expression is scoped to the expression.

var a = function b() {
 console.log(typeof b); //gives function
 console.log(typeof a); //gives function
};
console.log(typeof b); //gives undefined
console.log(typeof a); //gives function
a();

answered Feb 25, 2017 at 12:52
Sign up to request clarification or add additional context in comments.

1 Comment

To be 100% precise, there are two scopes involved. The first is the scope of the entire function expression; the second is that between the curly brackets. The scope of the name is the former. In other words, the following is valid, if useless: a = function b(arg = b) { };.
2

Why the difference in the two outputs?

You're taking a function expression for a function named b and assigning it to a variable named a. That means a is in scope where the expression occurs, but b is not; it's only in scope within the function. (The whole function, including the parameter list; that last part is only relevant for ES2015+, not ES5 and earlier: You can use b as the value of a default parameter.)

You probably expected b to be in scope where the expression was, because that's true for a function declaration:

function b() {
}
console.log(typeof b);

But this is just a difference in how function declarations and function expressions are handled.

answered Feb 25, 2017 at 13:00

5 Comments

it's only in scope within the function How do you define "within" here?
I would say within means between the curly braces after function
@gyre: Not just the body, the parameter list as well. The entire function.
Of course, assuming that we are speaking ES6. Not sure that was OP's intention but I know what you mean.
@gyre: :-) Probably not. But it's what torazaburo was getting at above.
-1

With

function b(){
}

you declare a function called b. This statement has no return value and therefor the return value is undefined.

To declare an anonymous function you have to omit the function name in the declaration, like this:

function(){
};

This one is just a function literal you can assign to a variable like that:

var a = function(){
};
answered Feb 25, 2017 at 12:53

1 Comment

This does not answer the question.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.