Strict mode in JavaScript

Last Updated :

For a long time, JavaScript had no compatibility issues because in every new version release old functionality were not changing. But it was not good as if there were any issue with old language, then old functionality must be modify accordingly to resolve that issue.

In this regard, ECMAScript 5 (ES5) has appeared in year 2009. It release new features and modified some of the existing ones for the first time. But, you will require a special directive: “use strict” to specify that you are using updated version of JavaScript. If you will not add “use strict” in your code, then such modifications are off by default.

“use strict”
This directive looks like a string: “use strict” or ‘use strict’. You need to inlcude it it Top of the Script to work correctly, otherwise it will be ignored and will not work.

For example:

"use strict";

// this code works the modern way
...

You can also use it as first line inside a function.

If you will include it middle of the script, then script mode will be ignore and will not be enable. So, strictly include “use strict” or ‘use strict’ in top of script to enable script mode.

Note that only comments may appear above “use strict”. Also, note that there is no way to revert the engine to old behavior once you enable strict mode.

Browser console
Browser console also does not enable strict mode by default, so you need to tell the browser to enable it.

But you need to use it along with code as top line of “use strict”. So you can do it by using multi-line code insertion.

You can hit Shift+Enter to input multiple lines

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

It works in most browsers, namely Firefox and Chrome.

If it does not work in your browser, then you can use it kind of wrapper to ensure work correctly. Include ‘use strict’ as first line of function:

(function() {
  'use strict';

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

You can “use strict” at the top of your scripts. But, if your code is organized into classes and modules, you can omit it. Becasue Modern JavaScript supports “classes” and “modules” that enable use strict automatically.

Comment
Mithlesh Upadhyay Published 25 Jun, 2023 · 2 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Article Tags :

Similar Reads

  • Code structure in JavaScript: Semicolon and Comment

    Semicolons and Comments are buidling blocks of code. Semicolons We can have as many statements in our code as we want. Statements can be separated with a semicolon.…

    3 min read
  • Hello, world! in JavaScript

    In this part of the tutorial, we will learn on core JavaScript. We need working environment o run our scripts. We will use either IDE or browser to…

    4 min read
  • Developer console in web browsers

    Code is prone to errors, and as a human, it’s natural to make mistakes. In the browser, errors are not visible to users by default, which makes it…

    2 min read
  • JavaScript Manuals and specifications

    This is a tutorial designed to help you learn the language gradually, but you’ll eventually need additional resources. 1. Specification The ECMA-262 specification is the most detailed and…

    2 min read
  • Code editors

    Code editors are where programmers spend most of their time. There are two main types of code editors: IDEs and Lightweight editors. IDEs (Integrated Development Environments) IDEs (Integrated…

    1 min read
  • An Introduction to JavaScript

    JavaScript was initially created to “make web pages alive”. These JavaScript program are called as scripts. These can be written in HTML of web page and will simply…

    4 min read