0

I have come across a very strange JavaScript code. Its execution result is completely different from what I expected. I want to know why.

var  a  =  1999; 
{
  console.log(a);
  a  =  2020;
  function  a()  {}
  a  =  2021;
}
console.log(a); //Why 2020?
mplungjan
  • 155,085
  • 27
  • 166
  • 222
huage
  • 11
  • 1
  • 1
    I think the relevant dupe target is [what-are-the-precise-semantics-of-block-level-functions-in-es6](https://stackoverflow.com/questions/31419897/what-are-the-precise-semantics-of-block-level-functions-in-es6) – ASDFGerte Jun 17 '21 at 07:41
  • This is one of the "black magic" behaviors in web-compat (non-strict, legacy emulation), and one of many reasons to always "use strict"; – ASDFGerte Jun 17 '21 at 07:43

0 Answers0