Yesterday, I clarified where to enable strict mode in your scripts. Today, let’s talk about the kinds of errors it catches.
What’s an example of an error that would be silent without [strict mode]?
Types of errors that would get ignored without strict mode include…
- Missing semicolons after variable declarations.
- Calling a variable that hasn’t be explicitly set.
- Redefining a variable that’s already been defined.
// Missing semicolon var someVar = 'this is my variable' // Undefined variable var anotherVar = 'another variable'; // No error anotherVar = 'change variable value'; // No error aThirdVar = 'a third variable'; // Error. Not yet been defined with `var` // Previously defined variable anotherVar = 'change variable value'; // No error. You're changing the value of a previously set variable var anotherVar = 'change the value again'; // Error. The variable has already been set, so you should leave off the `var`, which implies it's a new variable