|
| 1 | +# Use Strict in JavaScript |
| 2 | + |
| 3 | +What is use Strict ? |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +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. |
| 8 | + |
| 9 | +JS reads use strict 2 ways - as a string (as mentioned) or as a function (below) |
| 10 | + |
| 11 | +c |
| 12 | + |
| 13 | +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) |
| 14 | + |
| 15 | +## Strict rules & Changes |
| 16 | + |
| 17 | +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. |
| 18 | + |
| 19 | + |
| 20 | +### Defining Variables |
| 21 | + |
| 22 | +1. 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 |
| 23 | + |
| 24 | +- In the browser this is the window. |
| 25 | + |
| 26 | +- In Node this is an object called 'global'. |
| 27 | + |
| 28 | +### syntax |
| 29 | + |
| 30 | +```Javascript |
| 31 | + |
| 32 | +// in browser |
| 33 | +asim =1; |
| 34 | +console.log(window.asim) |
| 35 | +// in node |
| 36 | +console.log(global.asim) |
| 37 | +``` |
| 38 | + |
| 39 | +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). |
| 40 | + |
| 41 | +###example: |
| 42 | + |
| 43 | +```Javascript |
| 44 | + |
| 45 | +const value = 0 |
| 46 | + |
| 47 | +//pretend we have gone down 400 lines and attempt to call the value variable and reassign it like so: |
| 48 | + |
| 49 | +values = 1; |
| 50 | + |
| 51 | +if (value > 0 ) { |
| 52 | + console.log('Hello') |
| 53 | +} |
| 54 | + |
| 55 | +// 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 |
| 56 | +``` |
| 57 | + |
| 58 | +## Reserved Words |
| 59 | + |
| 60 | +2. 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. |
| 61 | + |
| 62 | +```Javascript |
| 63 | + |
| 64 | +Const let = 1; |
| 65 | + |
| 66 | +// Uncaught SyntaxError: Unexpected strict mode reserved word. |
| 67 | +``` |
| 68 | + |
| 69 | +## Cannot delete functions,variables or function arguements |
| 70 | + |
| 71 | +I.e - without strict mode you can delete functions variables or function arguements you would do it by doing this: |
| 72 | + |
| 73 | +``` Javascript |
| 74 | + |
| 75 | +const foo = 0; |
| 76 | +delete foo |
| 77 | + |
| 78 | +function moo(arguement) { |
| 79 | + delete arg; |
| 80 | +}; |
| 81 | +// these would work normally but in strict mode throws errors. |
| 82 | +``` |
| 83 | + |
| 84 | +## Calling eval('') |
| 85 | + |
| 86 | +eval() lets you run any code as an arguement as long as you pass it as a string. This can cause security issues. |
| 87 | + |
| 88 | +```Javascript |
| 89 | +var eval = 1; |
| 90 | +// causes error in strict mode |
| 91 | + |
| 92 | + |
| 93 | +eval("var a = 1"); |
| 94 | +console.log(a); |
| 95 | +// 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 |
| 96 | +``` |
| 97 | + |
| 98 | +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). |
0 commit comments