1

Although shadowing should never be used (or just to obfuscate) because it's confusing, I wanted to completely understand it. And I got this strange thing :

alert(parseInt('123'));//Here, I expected 123 but it's 'overshadowed'
function parseInt(){return 'overshadowed';}
alert(parseInt('123'));//Here it's 'overshadowed' too

Why the first alert output 'overshadowed' whereas the function is not modified yet?

P.S : I got inspired by Variable shadowing in JavaScript

Community
  • 1
  • 1
user1365010
  • 2,865
  • 7
  • 22
  • 42

1 Answers1

2

In JavaScript, all declarations are implicitly placed at the beginning of the scope ("hoisted"), so it doesn't matter if the parseInt() definition was at the second, last, or first line.

Alexander Pavlov
  • 30,691
  • 5
  • 65
  • 91
  • 2
    It's commonly referred to as "hoisting." http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting – ajm Jun 05 '12 at 12:39