What is use Strict ?
Use Strict allows you to place a function or program in what is called a "Strict Operating Context" Code that would usually fail silently or been ignored would not generate errors / throw exceptions. This directs you to errors in your code quicker.
You can enable this by typing it in (as a string) "use strict". These are not special keywords , it is a string. The reason why is because when it was implimented not all browsers supported it and if older browsers read it it would have failed.
JS reads use strict 2 ways - as a string (as mentioned) or as a function (below)
c
If you run it as a function essentially you would just put the functions in strict mode within the scope of the strictCode() function (or whatever you name it)
NTS: It seems like strict mode is similiar to using a linter.Except while a linter will call you out Strict will throw an error.
- Using a variable before it is defined throws an error. (Like with ESlint Airbnb config) in js there is always a global object where the global variables and functions go
-
In the browser this is the window.
-
In Node this is an object called 'global'.
// in browser
asim =1;
console.log(window.asim)
// in node
console.log(global.asim)
A problem with this is it can cause problems when calling variables, especially in larger files (i.e the kind you might use in production).
###example:
const value = 0
//pretend we have gone down 400 lines and attempt to call the value variable and reassign it like so:
values = 1;
if (value > 0 ) {
console.log('Hello')
}
// so as we might see here i might expect it to print hello and realized I meant to set VALUE not VALUES. if we were to use STRICT mode it would return an undefined error - in my personal case my ESlinter would cover this. But it is good to know when working in a professional setting.'use strict ' stops these syntax errors by calling them out for you
- As we know , let and const are words reserved for ES6 - lots of words are reserved for special use in JS , without strict mode this would not throw an error.
Const let = 1;
// Uncaught SyntaxError: Unexpected strict mode reserved word.
I.e - without strict mode you can delete functions variables or function arguements you would do it by doing this:
const foo = 0;
delete foo
function moo(arguement) {
delete arg;
};
// these would work normally but in strict mode throws errors.
eval() lets you run any code as an arguement as long as you pass it as a string. This can cause security issues.
var eval = 1;
// causes error in strict mode
eval("var a = 1");
console.log(a);
// above code can cause an issue with the namespace and the console log would not normally work as it is messing with its own block
Anyway with use strict any variables you print within eval stays within it so when you console.log it in the same manner as the example above - you throw an error (a is not defined).