Why can variable declarations always overwrite function declarations?
declaration, function, javascript, variables
Solution
It's to do with JavaScript's variable hoisting. Try this instead:
var a = 1;
var a = function() {};
typeof a // function
Problem
No matter whether I define the function after the variable ``` var a = 1; function a() {}; typeof a // number ``` Or if I define the function before the variable ``` function a() {}; var a = 1; typeof a // number ``` the final `typeof` result is always `number` I found some explanation about `execution context` in http://davidshariff.com/blog/what-is-the-execution-context-in-javascript/ ``` Before executing the function code, create the execution context. ...... Scan the context for variable declarations: If the variable name already exists in the variable object, do nothing and continue scanning. ``` but this does not seem to work. So how can I explain it?