YogeshChauhan.com

What’s a Strict mode in JavaScript?

March 9, 2021

Strict mode was introduced in ECMAScript 5. It’s a way to tell the browsers to run the code in a variant of JavaScript. The non-official term for the opposite of Strict mode is sloppy mode.

Strict mode has different semantics from the normal JavaScript code.

Be careful while using strict mode. If any browser or a particular browser version don’t support it then you might see unexpected output.

We can also use Strict mode code and non-strict mode code together.

What does the strict mode do?

When you change the JavaScript code to strict mode, it will make few changes to the JavaScript semantics.

First, it converts silent error to throw errors. If there are code mistakes then it makes difficult for JS engines to perform optimizations. Strict mode fixes those mistakes. We can make code run faster in strict mode rather than a sloppy mode.

How to apply the strict mode?

We can apply the strict mode to either entire script code or just to an individual function. modules use strict mode automatically .

strict mode doesn’t apply to block statements in {} braces.

What happens when we try to apply strict mode to block statements?

It won’t affect the code at all. It does nothing!

These are the entire scripts where strict mode works as expected.

  1. eval code,
  2. Function code,
  3. event handler attributes,
  4. strings passed to WindowTimers.setTimeout(), and
  5. related functions

How to apply Strict mode to scripts?

To apply strict mode for an entire script code, we can use the exact statement “use strict”; (or in single column ‘use strict’;) before any other code starts. So, at the top of the script code is the best place for it.

Add “use strict” to apply strict mode to an entire script.


"use strict";
var msg = "strict mode on";
console.log(msg);
//strict mode on

If you want to concatenate scripts then try to use script mode at function level rather than at script level. Concatenating strict script and sloppy script creates conflict in some cases.

How to apply Strict mode to functions?

Same exact way we did at script level. The difference is that we need to add the “use strict” statement in the function before any other statements.

Just like this:


function strictModeOn() {
  'use strict';
  return "strict mode is ON for me";
}
function strictModeOff() { 
  return "strict mode is OFF for me"; 
}

We can add nested functions in the strict mode and they all will use strict mode.

How to apply Strict mode to modules?

The JS modules (introduced in ES 2015) use the strict mode automatically. So, you don’t need to add the “use strict” statement.

dreamhost

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to uninstall Cocoapods from the Mac OS? #4 How to Use SQL MAX() Function with Dates? #5 How to add Read More Read Less Button using JavaScript? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Apr 7 Solution for “Yarn build: Failed because of a stylelint error” Apr 7 Make sure your links have descriptive text Apr 7 How to add Laravel to WordPress using Sage theme (and install Tailwind CSS)? Apr 2 How to create a sidebar using pure CSS? Apr 1 How to switch dark and light themes using pure CSS? Apr 1 How to calculate elapsed time in JavaScript?

You might also like these

Learn to Establish Connection using MySQLi (object-oriented), MySQLi (procedural) and PDO with Example CodePHPThe 8 Golden Rules of Programming I Have Learned in My CareerMiscWhat happened to your Google Analytics Tracking ID, can’t find it?MiscHow to uninstall Cocoapods from the Mac OS?MiscHow to Validate User Name, Email Address and URL in PHP?PHPSolution to Precision Problem in JavaScript NumbersJavaScript