0

I have this code:

var a = function(){
    a.data = "10";
};
    
function a(){
    a.data = "20";
};
a();
console.log("a data::", a.data);

I feel that the output should be 20, but it's 10 instead. Please help me understand the output. I've removed var but it's still showing the same output.

Jordan Running
  • 97,653
  • 15
  • 175
  • 173
Pankaj P
  • 43
  • 5
  • Possible duplicate of [Javascript function scoping and hoisting](https://stackoverflow.com/questions/7506844/javascript-function-scoping-and-hoisting) – Jordan Running Feb 09 '18 at 19:46

1 Answers1

1

Functions are hoisted to the top, so when you reassign a, it happens after the declared function a.

Essentially this is happening to your code:

function a() { ... }
// other variables and functions
a = otherFunctionA;
a();
Daniel A. White
  • 181,601
  • 45
  • 354
  • 430