JavaScript used to silently fail in contexts where what was done was an error. Strict mode throws in such cases. If your code base contains such cases, testing will be necessary to be sure nothing is broken. Once again, it can happen at the function granularity level.
Setting a value to an undeclared variable
function f(x) {
'use strict';
const a = 12;
b = a + x * 35;
}
f(42);
This used to change a value on the global object which is rarely the expected effect. If you really want to set a value to the global object, pass it as an argument and explicitly assign it as a property:
const global = this;
function f(x) {
'use strict';
const a = 12;
global.b = a + x * 35;
}
f(42);
Trying to delete a non-configurable property
'use strict';
delete Object.prototype;
In non-strict, this would silently fail, in contradiction with the user expectation.
Poisoned arguments and function properties
Accessing arguments.callee
, arguments.caller
, anyFunction.caller
, or anyFunction.arguments
throws an error in strict mode. The only legitimate use case would be to reuse a function as in:
const s = document.getElementById('thing').style;
s.opacity = 1;
(function () {
if ((s.opacity -= .1) < 0) {
s.display = 'none';
} else {
setTimeout(arguments.callee, 40);
}
})();
which can be rewritten as:
'use strict';
const s = document.getElementById('thing').style;
s.opacity = 1;
(function fadeOut() {
if ((s.opacity -= .1) < 0) {
s.display = 'none';
} else {
setTimeout(fadeOut, 40);
}
})();