What is Strict Mode in JavaScript?

What is Strict Mode in JavaScript?

JavaScript Strict Mode is a new feature in ECMAScript 5 that enables you to code a program, or a method, in a “strict” operating context. It is a way to opt into a restricted variant of JavaScript.

This strict context prevents certain actions from being taken and throws more exceptions. The statement “use strict”; instructs the browser to use the Strict mode, which is a reduced and safer feature set of JavaScript.

Benifits of using ‘use strict’

Strict mode makes several changes to normal JavaScript semantics.

  • Strict mode eliminates some JavaScript silent errors by changing them to throw errors.
  • Strict mode fixes mistakes that make it difficult for JavaScript engines to perform optimizations: strict mode code can sometimes be made to run faster than identical code that’s not strict mode.
  • Strict mode prohibits some syntax likely to be defined in future versions of ECMAScript.
  • It prevents, or throws errors, when relatively “unsafe” actions are taken (such as gaining access to the global object).
  • It disables features that are confusing or poorly thought out.
  • Strict mode makes it easier to write “secure” JavaScript.

How to use strict mode

  • Strict mode can be used in two ways – used in global scope for the entire script and can be applied to individual functions. Strict mode doesn’t work with block statements enclosed in {} braces.

Using Strict mode for the entire script

  • To invoke strict mode for an entire script, put the exact statement “use strict”; (or ‘use strict’;) before any other statements.
// Whole-script strict mode syntax
'use strict';
 let v = "strict mode script!";

NOTE: This syntax has a flow : it isn’t possible to blindly concatenate non-conflicting scripts. Consider concatenating a strict mode script with a non-strict mode script: the entire concatenation looks strict! The inverse is also true: non-strict plus strict looks non-strict. Concatenation of strict mode scripts with each other is fine, and concatenation of non-strict mode scripts is fine. Only concatenating strict and non-strict scripts is problematic. It is thus recommended that you enable strict mode on a function-by-function basis (at least during the transition period).

Using Strict mode for the entire script

Likewise, to invoke strict mode for a function, put the exact statement “use strict”; (or ‘use strict’;) in the function’s body before any other statements.

function strict() {

// Function-level strict mode syntax 'use strict';

function nested() { return 'Javascript on GeeksforGeeks'; }

return "strict mode function! " + nested(); } function notStrict() { return "non strict function"; }

Examples of using Strict mode

In normal JavaScript, mistyping a variable name creates a new global variable. In strict mode, this will throw an error, making it impossible to accidentally create a global variable

Using strict mode, don’t allow to use a variable without declaring it

// Using a variable, without declaring it, is not allowed:

 ‘use strict’;
 x = 3.14; // will throw an error

Output:

// Objects are variables too.
 // Using an object, without declaring it, is not allowed:
 ‘use script’;
 x = {p1:10, p2:20}; // will throw an error

Output:

Deleting a variable (or object) and a function is not allowed

‘use strict’;
 let x = 3.14;
// Deleting a function is also not allowed
 ‘use strict’;
 function x(p1, p2) {};
 delete x; // will throw an error

Output:

Duplicating a parameter name is not allowed

‘use strict’;
 function x(p1, p1) {}; // will throw an error

Output:

Octal numeric literals are not allowed

‘use strict’;
 let x = 010; // will throw an error

Output:

Escape characters are not allowed

‘use strict’;
 let x = \010; // will throw an error

Output:

Writing to a read-only property is not allowed

‘use strict’;
 let obj = {};
 Object.defineProperty(obj, “x”, {value:0, writable:false});
obj.x = 3.14; //will throw an error

Output:

Writing to a get-only property is not allowed

‘use strict’;
 let obj = {get x() {return 0} };
obj.x = 3.14; // will throw an error

Output:

Deleting an undeletable property is not allowed

‘use strict’;
 delete Object.prototype; // will throw an error

Output:

The string “eval” cannot be used as a variable

‘use strict’;
 let eval = 3.14; // will throw an error

Output:

The string “arguments” cannot be used as a variable

‘use strict’;
 let arguments = 3.14; // will throw an error

Output:

The with statement is not allowed

‘use strict’;
 with (Math){x = cos(2)}; // will throw an error

Output:

In function calls like f(), the this value was the global object. In strict mode, it is now undefined

In normal JavaScript, a developer will not receive any error feedback assigning values to non-writable properties

Thanks for reading

If you liked this post, please do share/like it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading about JavaScript

The Complete JavaScript Course 2019: Build Real Projects!

Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

JavaScript Bootcamp - Build Real World Applications

The Web Developer Bootcamp

JavaScript Programming Tutorial - Full JavaScript Course for Beginners

New ES2019 Features Every JavaScript Developer Should Know

Best JavaScript Frameworks, Libraries and Tools to Use in 2019

React vs Angular vs Vue.js by Example

Microfrontends — Connecting JavaScript frameworks together (React, Angular, Vue etc)

Creating Web Animations with Anime.js

Ember.js vs Vue.js - Which is JavaScript Framework Works Better for You

Do we still need JavaScript frameworks?

javascript web-development

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Web Developer

Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.

How long does it take to develop/build an app?

This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.