Assembly in Progress...

Strict Mode in JavaScript

Strict mode can be enabled by the developer to run a variant of JavaScript in the browser. This variant uses different semantics to execute JS code. Initiating this is as simple as writing: 'use strict';

One use case is as a temporary measure during development to uncover errors that would otherwise fail silently. Additionally, if browser support can be managed, it is possible to use this mode in production which, in some cases, will allow code to run faster.

Strict mode makes several changes to normal JavaScript:

  • Strict mode will throw errors instead of allowing some errors to fail silently.
  • Strict mode changes how the JavaScript engines perform optimizations, which can lead to the improved performance mentioned above.
  • Strict mode rejects syntax that is likely to be used in future ECMAScript versions.
↓ Example ↓
//    Setup the variable                                                    
const myVariable = {
    name: "John"
};

//    Freeze it preventing any changes
Object.freeze(myVariable);

//    Try to redfine the variable, whch will fail silently
myVariable.name = "Change after .freeze()"

console.log(myVariable); //    RESULT: "The Changed Name" because the Object was frozen

//    Opt in to strict mode.
'use strict';

//    Try to redfine the variable, whch will now throw an error instead of failing silently
myVariable.name = "Change after .freeze()"

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

q
↑ Back to Top