javascriptecmascript-6scopeuse-strict

Does "use strict" still work the same way in ES6?


I read an answer that "use strict" helps in restricting access to global variables and throwing unnecessary exceptions. But I just happened to wonder, whether "use strict" is still popular in ES6? OR is there an alternate (better) way today to achieving its functionality?


Solution

  • For the most part it does work the same and the "use strict" string is still required at the top of your code to enter strict mode, even if you are using ES6 features. For example, if the code will be executed by a browser, the browser will not evaluate code in strict mode unless you use that string at the top of your code.

    The only two exceptions are ES6 modules which automatically use strict mode, and ES6 classes, where any code in the body of the class is executed in strict mode.

    You can find more details here at the MDN docs about Strict Mode and the docs about Classes