use strict in JavaScript


Problem

Ever wondered what use strict does in JavaScript?



Solution

use strict is a feature of ECMAScript 5 allowing you to put a block in a strict execution mode. This can help at finding certain problems at an earlier time.

  • It prevents you to access global objects and other unsafe operations.
  • It ensures that you always define your variables.
  • It avoids bad coding style by throwing exceptions for them.
function strictFunction() {
   // put function in strict mode
   'use strict';

   return 'Hello from strict mode!';
}

function notStrictFunction() {
   return 'Hello from not strict mode'; 
}

A more detailed explanation can be found here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode

Comments




Please sign-in to post a comment