1

I encountered some confusing issue with using typeof function when the function is declared or when the function is defined as a variable assignment. Here is a sample code:

     //This displays "undefined"
     console.log(typeof window['a']);

     //This displays "function"
     console.log(typeof window['b']);

     function a() {};
     b = function() {};

EDIT: This has nothing to do with hoisting. I have added the following equivalent Node.js code, which is complete so there are no doubts about what else is going on in the program:

     function a() {}; 
     b = function() {}; 
     console.log(typeof global['a']);
     console.log(typeof global['b']);

The output is:

    undefined
    function

Can someone explain in light of the above updates why a function declared has no entry in the global/window object?

This is NOT a duplicate. The other questions do not address this specific issue about the returned value of typeof when function is declared as a function or as a variable assignment.

asinix
  • 766
  • 6
  • 15
  • 1
    This is known as Hoisting : https://developer.mozilla.org/en-US/docs/Glossary/Hoisting – Seblor Jul 07 '19 at 15:53
  • 1
    Cannot reproduce. – Jonas Wilms Jul 07 '19 at 16:05
  • @JonasWilms Please see update – asinix Jul 07 '19 at 16:38
  • 1
    Yes it is a duplicate, hoisting *does* explain the behaviour. As to why your second snippet does not work, that's a separate issue: `a` is not a global variable - see [In what scope are module variables stored in node.js?](https://stackoverflow.com/q/15406062/1048572). – Bergi Jul 07 '19 at 16:47
  • 1
    Now you are mixing even more things. `global` and `window` are fundamentally different. I still can't reproduce your first example. – Jonas Wilms Jul 07 '19 at 16:47
  • @Bergi I was not aware of the difference in scope global vs. module in node.js. Thanks – asinix Jul 07 '19 at 16:52

2 Answers2

1

Function deceleration

When you define a function it will be hoisted (not only name but also it's definition ) and you will be able to access it even before the compiler reaches ( During execution context ) to the line where it is defined

Function expression

when you define function expression the variable will be hoisted and will have value as undefined you will be able access function only after the line it is defined ( During execution context )

console.log(typeof window['a']);
console.log(typeof window['b']);

function a() { return 'a'};
b = function(){ return 'b'};
Code Maniac
  • 33,907
  • 4
  • 28
  • 50
  • 2
    But, isn't this the opposite of what the OP reports. They say they're getting `undefined` for the hoisted function name. – jfriend00 Jul 07 '19 at 16:01
  • @Code Maniac Please see my update – asinix Jul 07 '19 at 16:35
  • @RTC now this is completely different than original posted one as, bergi has provided a dup target for it [In what scope are module variables stored in node.js?](https://stackoverflow.com/questions/15406062/in-what-scope-are-module-variables-stored-in-node-js) – Code Maniac Jul 07 '19 at 16:58
0

This is a case of Function declaration vs Function expression.

Function Declaration

A declared function is “saved for later use”, and will be executed later, when it is invoked (called). Just as Variable Declarations must start with “var”, Function Declarations must begin with “function”

 function a() {...};

thats why typeof a returns undefined as it has not been used yet.

Function Expressions

A function expression can be stored in a variable:

 b = function {...};

After a function expression has been stored in a variable, the variable can be used as a function. Functions stored in variables do not need function names. They are always invoked (called) using the variable name.

when you do typeof b : It comes as a function, as it is already been stored in a window variable.

For detailed information you can read this Medium post

Ravi Shankar Bharti
  • 7,666
  • 4
  • 21
  • 51