0

Why isn't "use strict" the default way to interpret ES6 javascript, preferring the functionality: "do not use strict" (or whatever) to turn it off?

Previously, I imagined "use strict" as sort of like Safe Mode in macOS (only to be turned on when necessary, and then only temporarily), but I have since learned that it is recommended by many developers to use "use strict" for basically everything in javascript.

So, if it's the recommended default, why isn't it an actual default for interpreters?

Pointy
  • 405,095
  • 59
  • 585
  • 614
PJM Design
  • 150
  • 5
  • 9
    Because the ECMAScript committee did not want to break the entire Internet. There are scripts out there from 2002 and earlier. – Pointy Dec 16 '19 at 22:14
  • 2
    If it helps, it's the default for all new scopes that were not available up to ES5. For example if you create a class or if you create a module - they are automatically in strict mode. – VLAZ Dec 16 '19 at 22:18
  • Use TypeScipt, it's better than ES6 and it is the default. – Adrian Brand Dec 16 '19 at 22:18
  • Man developers today would not last in the document.all vs document.layers wars back in the day. ;) – epascarello Dec 16 '19 at 22:21
  • @Pointy what kind of things specifically would be broken that were done in the past which would be broken by "use strict" now? – PJM Design Dec 16 '19 at 22:22
  • 2
    @PJMDesign Everything that use strict enforces, tons of scripts fail those checks. Things used before they are declared, people not using `var`, people relying on id of element instead of document.getElementById – epascarello Dec 16 '19 at 22:22
  • 1
    @PJMDesign `this` being `undefined` is a really big one, for example. – VLAZ Dec 16 '19 at 22:22
  • Also implicit globals. – Pointy Dec 16 '19 at 22:57

1 Answers1

3

For backward compatibility. Old code that doesn't opt in to strict mode must still work.

Notice however that the main new ES6 features actually do imply strict mode, so yes it is on by default in modern code (which uses ES6 module syntax).

Bergi
  • 630,263
  • 148
  • 957
  • 1,375