Skip to content
Advertisement

Only in Safari: ReferenceError Can’t find variable

Many of my scripts look like this:

JavaScript

They work fine on Chrome, Firefox, Edge and Opera but on Safari I get the error:

JavaScript

Workaround

If I declare the constants before the if statement the code works…

JavaScript

…but I don’t understand why and I don’t what to make the constant globally available.

Maybe someone can explain to me that Safari-only-behavior.

Advertisement

Answer

This weird behaviour is explained in Block-level functions in non-strict code – MSN.

Enable strict mode will solve this problem.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement