Skip to content

Latest commit

 

History

History
147 lines (114 loc) · 4.58 KB

strict-mode.md

File metadata and controls

147 lines (114 loc) · 4.58 KB

strict mode

The modern mode, "use strict"

For a long time, JavaScript evolved without compatibility issues. New features were added to the language while old functionality didn’t change.

That had the benefit of never breaking existing code. But the downside was that any mistake or an imperfect decision made by JavaScript’s creators got stuck in the language forever.

This was the case until 2009 when ECMAScript 5 (ES5) appeared. It added new features to the language and modified some of the existing ones. To keep the old code working, most such modifications are off by default. You need to explicitly enable them with a special directive: "use strict".

"use strict"

The directive looks like a string: "use strict" or 'use strict'. When it is located at the top of a script, the whole script works the “modern” way.

For example:

"use strict";

// this code works the modern way
...

Note:

"use strict" can be put at the beginning of a function. Doing that enables strict mode in that function only. But usually people use it for the whole script.

Browser Console

When you use a developer console to run code, please note that it doesn’t use strict by default.

Sometimes, when use strict makes a difference, you’ll get incorrect results.

So, how to actually use strict in the console?

First, you can try to press Shift+Enter to input multiple lines, and put use strict on top, like this:

'use strict'; <Shift+Enter for a newline>
//  ...your code
<Enter to run>

It works in most browsers, namely Firefox and Chrome.

If it doesn’t, e.g. in an old browser, there’s an ugly, but reliable way to ensure use strict. Put it inside this IIFE(Immediately Invoked Function Expression):

(function() {
  'use strict';

  // ...your code here...
})()

Example

Demonstration of IIFE without "use strict";:

(function() { // begin IIFE  

  // first function: no parameters, no return value
  var hello = function() {
    alert("Hello, SLO. Let's calculate a rectangle");
  };

  // second function: one parameter, with return value
  var getNumber = function(instructions) {
    number = +prompt(instructions); 
    /*
       Here number is used without declation which should give an error but it wouldn't as this 
       IIFE doesn't use any "use strict" directive
    */
    if (isNaN(number)) {
      return 0;
    }
    return number;
  };

  // third function: two parameters, no return value
  var rectangleArea = function(x, y) {
    var area = x * y;
    alert("The area is " + area);
  };

  // main function
  var main = function() {
    hello();
    var width = getNumber("Enter width");
    var length = getNumber("Enter length");
    rectangleArea(width, length);
  };

  main();
})(); // end IIFE

Demonstration of IIFE with "use strict";:

(function() { // begin IIFE  
  "use strict"; // use strict directive
  // first function: no parameters, no return value
  var hello = function() {
    alert("Hello, SLO. Let's calculate a rectangle");
  };

  // second function: one parameter, with return value
  var getNumber = function(instructions) {
    number = +prompt(instructions); 
    /*
       Here number is used without declation which will give an error as this IIFE use 
       "use strict" directive
    */
    if (isNaN(number)) {
      return 0;
    }
    return number;
  };

  // third function: two parameters, no return value
  var rectangleArea = function(x, y) {
    var area = x * y;
    alert("The area is " + area);
  };

  // main function
  var main = function() {
    hello();
    var width = getNumber("Enter width");
    var length = getNumber("Enter length");
    rectangleArea(width, length);
  };

  main();
})(); // end IIFE

Should we "use strict"?

The question may sound obvious, but it’s not so.

One could recommend to start scripts with "use strict"… But you know what’s cool?

Modern JavaScript supports “classes” and “modules” – advanced language structures (we’ll surely get to them), that enable use strict automatically. So we don’t need to add the "use strict" directive, if we use them.

So, for now "use strict"; is a welcome guest at the top of your scripts. Later, when your code is all in classes and modules, you may omit it.

Some links for details about "use strict";:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

https://www.geeksforgeeks.org/strict-mode-javascript/