"use strict"; in javascript

"use strict"; in javascript

What does ‘use strict’ do?”. This is the kind of question that you think you know but then you realize that you just have an intuition of what ‘use strict’ does. Here's an article that discusses "use strict" in detail.

Hey guys, so full disclosure; I have been working as a product enginner and I had the task to quickly prototype a feature and a long story short I basically asked a few of the other memebers for help since I couldn't get a certain POST request to work right, and after a few hours in and since all of them said the way the POST request was being made is pretty much right, I decided to debug the application and by this moment the application was extremely complex and branched. So just when I was about to begin, one of my coworkers asked if he could checkout the code and as i scrolled to the POST request i realisied I had misspelled a variable and my coworker saw that and thats when he told me about "use strict" so that's pretty much when I took a moment to read up about it and thought it's something that everyone starting javascript should definetely know. So let's get started and also I started a youtube channel about a month back and this article is summarizing the video, if you prefer video format check that out.

The "use strict" directive was new in ECMAScript version 5 (around since 2011) and forces a program (or function) to work under a “strict” operating context. It is not a statement, but a literal expression, ignored by earlier versions of JavaScript. With strict mode on it is easier to detect JavaScript silent errors as they would throw an error now. This makes debugging much easier and the chances of developers making mistakes is reduced. Most modern browsers support strict mode except Internet Explorer 9.

so why exactly is "use strict" a string ?

If an older browser saw the "use strict" string it would consider it as a string and simply ignore it executing without enabling the strict mode as it is just a string Internet Explorer 9 will not throw an error even if it does not understand it, in a new browser when it sees the keyword used strict it then knows to turn itself into strict mode operating.

enabling strict mode !

Strict mode can be used in two ways

  • used in global scope

    // Whole-script strict mode syntax
    'use strict';
    var v = "Hi! I'm a strict mode script!";
  • used in functions

    // not strict
    function strict() {
        // Function-level strict mode syntax
        'use strict';
        return "Hi!  I'm a strict mode function!  ";
    // not strict

"strict mode"; reports errors on the following:

  • Using a variable, without declaring it.
  • Using an object, without declaring it.
  • Using reserved keywords as variable names.
  • Deleting a variable (or object) is not allowed.
  • Deleting a function is not allowed.
  • Duplicating a parameter name is not allowed.
  • The word eval cannot be used as a variable.
  • For security reasons, eval() is not allowed to create variables in the scope from which it was called.
  1. Using variable / object wihout declaring it. ( helps is you misspelled a variable )

    'use strict';
    var uniqueValue = 23;
    uniquValue = 17;         // this line throws a ReferenceError due to the 
                                                                            // misspelling the variable
    unknownValue = 25;      // this line also throws a ReferenceError as variable is not declared

    Would result in a new global variable (as in unknownValue and uniquValue) created, and uniqueValue would remain untouched. Under strict mode, assigning a value to an undefined variable will throw a ReferenceError.

  2. Using reserved words to declare variables. ( future proof your code )

    var let = 24;
    console.log(let) // will output 24
    "use strict";
    var let = 24;
    console.log(let) // will raise an error

    Keywords reserved for future JavaScript versions can NOT be used as variable names in strict mode. These are: implements,interface,let,package,private,protected,public,static,yield.

  3. Deleteing variables, object, functions in strict mode raises error.

    "use strict";
    var variable = 23;
    var obj = { 'name':'mxiv' };
    function func(p1, p2 ) {
    delete variable; // will raise error
    delete obj; // will raise error
    delete func; // will raise error
  4. Function arguments cannot be deleted and have same name

    "use strict";
    function func(p1, p2 ) {
        delete(p1); // raises error
    function func2(p1,p1) { // raises error
  5. Eval restrictions.

    eval('var variable = 2;');
    console.log(variable); // displays 2 as the variable is leaked out of the eval function.

    when strict mode is used eval does not leak variables declared in the expression passed to it. hence for security reasons in strict mode eval is not allowed to create variables in the scope from which it was called this brings us to the end of this article.

If you liked the content checkout my youtube channel - YouTube - use strict in javascript .

javascript node-js web-development reactjs html

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 Node.JS Developers | Skenix Infotech

We are providing robust Node.JS Development Services with expert Node.js Developers. Get affordable Node.JS Web Development services from Skenix Infotech.

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

Top Node.js Development Companies and Expert NodeJS Developers

A thoroughly researched list of top NodeJS development companies with ratings & reviews to help hire the best Node.JS developers who provide development services and solutions across the world. List of Leading Node.js development Service Providers...

How to SETUP a WEB SERVER in Node.js, Express, JavaScript & HTML

Hey guys and gurls. I'll be showing you how to create a simple server using express in node.js. This is a super simple concept I believe everyone who's down