JavaScrip Strict Mode (use strict)
The strict mode was introduced in ES5. It is a way of enforcing strict rules while coding in JS. Not all browsers support strict mode, so always test your code.
Advantages:
- Disable some silent JS errors and throw them instead.
- Performant code in specific instances where a JS engine supports performance optimizations
- Code improvements
- No duplicate keys in objects
- Variable declaration without
var
keyword - Error on duplicate function arguments
Enabling strict mode
- File-level: add
"use strict"
on top of the file before any other statements. - Function level: add the same
"use strict"
on top of the function body before any other statements. - Module level: Modules introduced in ES6/ES2015 are in strict mode by default.
Changes applied in strict mode
- Variable declaration without
var
keywordsomeVariable = 17; console.log(someVariable); // 17
This is a completely valid code. Even though we did not use
let
,var
orconst
to declare the variable, JavaScript works without an issue. This would force JS to create a global property, and that could cause side effects in a large application (variable name conflict and modifying global variables).Strict mode solves this by throwing an error.
- No duplicate keys in objects
const myObject = { name: "Parwinder", age: 34, car: "MiniCoop", name: "Bhagat" } console.log(myObject.name); // Bhagat
Again, completely valid under non-strict mode (sloppy mode) but throws an error in strict mode.
- Throw error when trying to delete undeletable properties of an object
"use strict" delete Object.prototype; // throws a TypeError
- No duplicate arguments in functions
function sumOfNumbers(a, a, b) { return a + a + b; } console.log(sumOfNumbers(1, 2, 3)); // 7
The above code is valid too. It ends up with unexpected results. The value of
a
is set to 2, and the sum returns 7 instead of our expectation, 6. Strict mode throws a syntax error. - Assigning
NaN
to a variable has no failure feedback in sloppy mode. Strict mode throws an exception
🚨 There’s no way to cancel “use strict”. Once we enter strict mode, there’s no way to turn it off.