0

This is the problem I see when browsing javascript.info, the expected result should be an "error", as in the website.

Related Links

  let phrase = 'Hello';
  if(true) {
    let user = "John";

    function sayHi() {
      console.log(phrase, user);
    }
  }

  sayHi();

When I tried it, I found that both Chrome and Firefox output "Hello John" normally. But the error in Safari is "ReferenceError: Can't find variable: user"

I don't know how to describe this behavior, so I can't find relevant information in the search engine. I hope someone can answer my doubts. Thank you!

Browser version:

  • Chrome 110
  • Firefox 109
  • Safari 16.3

This question seems to be different from here

Simao
  • 171
  • 1
  • 4
  • Related: [Function declarations inside if/else statements?](/q/10069204/4642212). – Sebastian Simon Feb 12 '23 at 16:12
  • 2
    "*I don't know how to describe this behavior*" for Safari seems like the correct description would be "bug". Not that it's really much better in other browsers as web compatibility certainly has odd and unintuitive semantics. – VLAZ Feb 12 '23 at 16:14
  • 2
    javascript.info expects you to run in strict-mode, which you also should. There, it is an error. ["All examples in this tutorial assume strict mode unless (very rarely) specified otherwise."](https://javascript.info/strict-mode#should-we-use-strict) Perhaps they should have made that bold, red, and fontsize 48. – ASDFGerte Feb 12 '23 at 17:09

0 Answers0