Some devs would probably describe javascript as a flexible language. Flexible, in this case, would refer to JS allowing certain lines of code that don't fall under 'proper syntax' to run without issue. I've often heard this action be referred to as failing silently. 'Strict mode' could be used to combat this flexible behavior in a lot of cases. You could think of strict mode as a sort of JavaScript compass, pointing you in the general direction of your error.
An example of not using strict mode vs. using strict mode could be seen with the variable assignment. Not having strict mode while attempting to create a variable like this:
love = "Shovel Knight and Shield Knight" ;
love;
Would run with JS considering the love variable a global variable created with the var keyword. With 'strict mode' set at the beginning of the script that variable assignment would not be allowed and JS would throw a reference error, showing you where the mistake was made.
'use strict' ;
love = "Shovel Knight and Shield Knight" ;
This would throw a reference error stating that love isn't defined. That error should let me know that I need to define the love variable with a keyword of either let or const. The var keyword can be considered as well but, for REASONS...... ahem, we will just stick with let and const.
Since realizing this was an actual thing in JS, I've been trying to make use of it as much as I can. It's helped me to get better with my syntax and there's so much more that I can do than help you avoid bad variable assignments. Give it a try if you get a chance
Top comments (0)