JavaScript Strict Mode: What it Does and Doesn’t AllowJavaScript Strict Mode: What it Does and Doesn’t Allow

In this tutorial, you will learn about the strict mode in JavaSript, the process of activating strict mode for a program or an individual function in JavaScript, and some typical actions that are forbidden by strict mode in JavaScript.

Activating JavaScript Strict mode

For activating Strict mode, you can add ‘use strict’; or “use strict”; at the beginning of the script.

Syntax:

‘use strict’;// or"use strict";

This statement should be the first statement in the script excluding comments as JavaScript just ignore them.

If there is any other statement before ‘use strict’; or “use strict”; then string mode will not activate.

Adding ‘use strict’; or “use strict”; at the beginning of the script has a global scope which makes all the code in that script executed in string mode.

Example:

"use strict";name = "John";console.log(name);

Output:

name = "John";

ReferenceError: name is not defined

Without Strict Mode:

name = "John";console.log(name);

Output:

John

Also Read:

Leave a Reply

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

Share this article:

RSS2k
Follow by Email0
Facebook780
Twitter3k
120
29k
130k

Also Read: