JAvascript

Strict Mode in Javascript

Strict mode

Strict mode is restricted variant of JavaScript introduced in ECMAscript 5 to make some changes to normal JavaScript semantics, such as:

  • eliminating some JavaScript silent errors by changing them to throw errors.
  • introducing script optimisation to make the code run faster.
  • eliminating some syntax that is likely to unsupported in future versions of ECMAScript.

Enabling strict mode

Writing "use strict"; on top of a script enables strict mode for the entire script. Make sure that the directive "use strict" is on the top of the script, otherwise the strict mode will not be enabled.

One common error beginners make is:

//Some code
var top = 1

// "use strict" is ignore because it is not at the top.
"use strict";

However comments are allowed above "use strict".

Proper way to enter strict mode:

//script level strict mode
"use strict";

// whatever script you have
var start = "Welcome to strict mode";

Strict can be applied to entire scripts or to individual functions. However doesn’t work on block statements enclosed in {} braces.

To invoke strict mode for a function, put the statement "use strict"; in the function’s body at the top before any other statement.

function func1() {
  // function level strict mode
  
  "use strict";
  // from here everything in strict mode, even nested functions.
  }

function func2() {
   // not in strict mode
   }

How strict mode works

JavaScript was designed with some loose syntax to make it tolerate to beginners. The idea was to make it easy to novice developers by making "non fatal" mistakes silent. Sometimes this is welcome but sometimes these ignored mistakes create worse problems later.

Another thing about JavaScript is that as new features were constantly being added to the language over the years, the old functionality did not change. The reason for that was to avoid breaking existing code. However the biggest disadvantage for that was that any mistake made by JavaScript creators got stuck in the language forever.

The introduction of ECMAScript 5 (ES5) in 2009 added some new modern features to JavaScript. But as we mentioned earlier, there was need to avoid breaking the old code, so most of the modifications were turned off by default and can be turned on by invoking strict mode.

Strict mode was introduced to convert old mistakes (mistakes that were tolerated in old JavaScript) into errors so that they can be quickly discovered and fixed. As an example, lets look at this classic JavaScript mistake:

// declare variable 
let thruster_temperature = 2303;

// assign a new value a misspelt variable (by mistake)
thruster_temerature = 3387;

If you run the code above in non-strict mode, not error or warning will be raised. That is because when you misspelt the variable JavaScript create a new undeclared variable thruster_temerature when yet you wanted to assign the value to variable thruster_temperature.

In strict mode this becomes an error because undeclared variables are not allowed.

"use strict"
// declare variable 
let thruster_temperature = 2303;

// assign a new value a mispelt variable (by mistake)
thruster_temerature = 3387; // ReferenceError: thruster_temerature is not defined