'use strict'
shouldn’t cause you any problems'use strict'
var
(or let
or const
) keyword, it’ll throw an error (because you didn’t define a scope, var
/let
/const
define the scope of the variable)this is bad
1badVariable = 35
it’s bad because you didn’t use the var
(or let
or const
) keyword, which are needed to define the scope of the variable. You have cluttered the global name space by creating a global variable.
Now this bad code will still run normally, but if you use strict
, it’ll fail and give you an error
1'use strict';
2badVariable = 35
3console.info(badVariable) // Uncaught ReferenceError: badVariable is not defined(…)
1NaN.foobar = true; // Uncaught TypeError: Cannot create property 'foo' on number 'NaN'(…)
NaN
is just an object used as example here, you wouldn’t actually write the above code.
1delete Object.prototype //Uncaught TypeError: Cannot delete property 'prototype' of function Object() { [native code] }(…)
1let someOctal = 500 + 090;
2console.info(someOctal) // Uncaught SyntaxError: Octal literals are not allowed in strict mode.
delete
can not be used to delete variables or plain objects (you can only Object properties)1let cutie = 'i am cute'
2delete cutie // Uncaught SyntaxError: Delete of an unqualified identifier in strict mode.