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 |
One Response
I congratulate, what words…, a magnificent idea