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
  • 3,185
  • 8
  • 24
  • 43

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
  • 31,598
  • 5
  • 67
  • 93
  • 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