Skip to content

Latest commit

 

History

History
48 lines (39 loc) · 1.1 KB

File metadata and controls

48 lines (39 loc) · 1.1 KB

'use strict';

Put this at the top of your script files when you are not using any build tools and reap benefits.

There are actually a lot of things that this declaration does, but here are some.

Many modern JS environments have this enabled by default, or similar rules in place.

Setting undeclared variables becomes an error

'use strict';
value = 42; // throws ReferenceError

Setting parameters becomes an error

'use strict';
function fn (a) {
  a = 42; // throws
}

Duplicate object keys becomes an error

'use strict';
var o = { p: 1, p: 2 }; // !!! syntax error

Setting properties on primitives becomes an error

You can use 'use strict' within function scope.

(function() {
  'use strict';
  false.true = '';         // TypeError
  (14).sailing = 'home';   // TypeError
  'with'.you = 'far away'; // TypeError
})();

Deleting plain names becomes error

'use strict';
var x;
delete x; // !!! syntax error
eval('var y; delete y;'); // !!! syntax error