Skip to content
Home » Strict mode in javascript and its use cases

Strict mode in javascript and its use cases

Js strict mode

In this article, we will learn about Strict mode in javascript and its use cases.


What is strict mode in javascript

  • It’s a new feature in ECMAScript 5 or ES5.
  • It allows javascript code to execute in a “strict” operating context which prevents certain actions and throws exceptions.
  • It is a reduced and safer feature set of JavaScript.
  • The statement "use strict"; instructs the browser to use the Strict mode.
  • Since “use strict” is just a string, IE 9 will not throw an error even if it does not understand it.

Why to use strict mode

  • It changes previously accepted bad syntax into errors.
  • Makes it easier to write secure JavaScript.
  • Disables confusing or poorly thought out features.
  • Sometimes scripts can execute faster in strict mode.
  • Prevents or throws errors for unsafe actions like gaining access to the global object.
  • Eliminates some JavaScript silent errors by changing them to throw errors.
  • Fixes the problems that make it difficult for JavaScript engines to perform optimizations.
  • Prohibits some syntax likely to be defined in future versions of ECMAScript.

How to use strict mode

There are two ways to use it:

  • In global scope:
    • Use Strict Mode for the entire script.
    • For this, just out exact statement "use strict"; at the start of the script that is before any other statements.
// Strict mode syntax for entire script. 
// So codes below the line 'use strict' will be executed in strict mode.
'use strict';
let aVariable = "strict mode script started!";
  • Local scope:
    • Use Strict Mode for individual functions.
    • For this, just put the exact statement "use strict"; in the function’s body as the first line of the code that is before any other statements.
    • Note: It doesn’t work with block statements enclosed in {} braces.
// Strict mode syntax is used inside the function so only the codes present inside the function will execute in strict mode.
function strictMode() {
    'use strict';

    function innerFunction () {
        return 'Javascript Strict mode';
    }

    return "Function executed in strict mode! " + innerFunction();
}

// Strict mode syntax is not used in below function so it will execute without strict mode.
function nonStrictMode() {
    return "non strict function";
}

Use cases of strict mode with example

  • Does not allow creating a global variable.
    • It will not allow using a variable without declaring it.
    • So must declare a variable with the declarations like var, let, etc.
// Example: 1
'use strict';
testString = 'test';  // It will throw an error as "Uncaught ReferenceError: testString is not defined"

// Example: 2
'use strict';
testObject = { a:1, b:2 }; // It will throw the same error as above.

// Example: 3
'use strict';
var testObject = { a:1, b:2 }; // It will not throw any error.

// Example: 4
testString = 'test'; // No error as the code is not under strict mode.

testFunc();

function testFunc() {
    'use strict';
    testObject = { a:1, b:2 }; // Throws error as the code is under strict mode.
}
  • Does not allow deleting a variable or object or a function.
// Example: 1
'use strict';
var testString = 'test';
delete testString; // Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.

// Example: 2
'use strict';
function testFunc(x, y) {};
delete testFunc; // Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.

// Example: 3
'use strict';
var testObject = { a:1, b:2 };
delete testObject; // Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.
delete testObject.a; // Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.
  • Does not allow duplicating a parameter name in function.
'use strict';
function testFunc(x, x) {}; // Uncaught SyntaxError: Duplicate parameter name not allowed in this context
  • Octal numeric literals are not allowed.
'use strict';
var a = 010; // Uncaught SyntaxError: Octal literals are not allowed in strict mode.
  • Escape characters are not allowed.
'use strict';
var a = \010; // Uncaught SyntaxError: Invalid or unexpected token
  • Writing to a read-only property is not allowed.
'use strict'; 
var testObject = {}; 
Object.defineProperty(testObject, 'a', {value: 0, writable: false});
testObject.a = 123; // Uncaught TypeError: Cannot assign to read only property 'a' of object '#<Object>'
  • Writing to a get-only property is not allowed.
'use strict'; 
var testObject = {
    get a() {return 0}
};

testObject.a = 123; // Uncaught TypeError: Cannot set property a of #<Object> which has only a getter
  • Deleting an undeletable property is not allowed.
'use strict';
delete Object.prototype; // Uncaught TypeError: Cannot delete property 'prototype' of function Object() { [native code] }
  • The string “eval” cannot be used as a variable.
'use strict';
var eval = 123; // Uncaught SyntaxError: Unexpected eval or arguments in strict mode
  • The string “arguments” cannot be used as a variable.
'use strict';
var arguments = 123; // Uncaught SyntaxError: Unexpected eval or arguments in strict mode
  • The with statement is not allowed.
'use strict';
with (Math) { x = sqrt(4) }; // Uncaught SyntaxError: Strict mode code may not include a with statement
  • The this keyword in functions behaves differently in strict mode.
    • this keyword refers to the object that called the function so if the object is not specified, functions in strict mode will return undefined and functions in normal mode will return the global object (window):
'use strict';
function testFunc() {
    console.log(this); // It will print 'undefined'
}

testFunc();
  • Can not use reserved keywords in strict mode. below are some:
    • implements
    • interface
    • let
    • package
    • private
    • protected
    • public
    • static
    • yield

Should we use strict mode in every application?

  • Modern JavaScript supports classes and modules that enable the use of strict automatically.
  • So if use classes or modules then we don’t need to add the “use strict” directive specifically.

I hope you like this article and helps you to solve your problems.

Visit Techtalkbook to find more related topics.


Leave a Reply

Your email address will not be published. Required fields are marked *

1 Shares
Tweet
Pin1
Share
Share
Share