0

JavaScript in Safari fails and execution is stopped if const is used within function. All other browsers (Chrome, Firefox, ...) work correctly, even on macOS.

My code (simplified):

const a = 1;

function f() {
  console.log(a);
}
f();

In JavaScript console I can see: [Error] ReferenceError: Can't find variable: a

I resolved it by changing const to var and it started to work immediately. (I didn't try let).

    var a = 1;
    function f() {
      console.log(a);
    }
    f();

I tried to investigate and understand why this is a problem. However, none of the resources gave me an answer why const should not work within function:

Is it something to do with Safari (security?) settings?

Unfortunately, I can't troubleshoot this further as I'm working on Windows and don't have macOS available all the time.

Barmar
  • 741,623
  • 53
  • 500
  • 612
CraZ
  • 1,669
  • 15
  • 24
  • I tested this in Safari Version 15.1 and it works as expected with `const`. Are you using on old Windows version of Safari? – James Dec 15 '21 at 20:03
  • No, this was based on reports of our macOS users (latest Safari). Afaik, Safari on Windows was discontinued several years ago. – CraZ Dec 18 '21 at 21:37
  • 1
    I can't reproduce it with the code you posted. But if I put that code inside an `if` block, the error occurs. It seems that you simplified it too much. – Barmar Apr 15 '22 at 01:34
  • Indeed sounds very much like https://stackoverflow.com/questions/57243751 (always "use strict"). – Kaiido Apr 15 '22 at 01:48
  • @Barmar, yes, possibly, I might have simplified it too much. I'll try to to reproduce it again with `if` instead of `function`. – CraZ Apr 17 '22 at 09:12

0 Answers0