myVariable = 9;
'use strict'; // Error myVariable = 9;
The above code throws an error because myVariable is not declared. In strict mode, you cannot use the variable without declaring them.
To indicate this program is in the strict mode, we have used
at the top of the program.
You can declare the strict mode by adding 'use strict'; or "use strict"; at the beginning of a program.
When you declare strict mode at the beginning of a program, it will have global scope and all the code in the program will execute in strict mode.
Strict Mode in Variable
In strict mode, using a variable without declaring it throws an error.
Note: You need to declare strict mode at the beginning of the program. If you declare strict mode below some code, it won't work.
console.log("some code"); // 'use strict' is ignored // must be at the top "use strict"; x = 21; // does not throw an error
Benefits of Strict Mode
The use of strict mode:
helps to write a cleaner code
changes previously accepted silent errors (bad syntax) into real errors and throws an error message