If you’re just learning JavaScript, or you’re reading through the codebase of a JavaScript library, chances are that you’ve seen the funny ‘use strict’ or “use strict” directive.
The ‘use strict’ command enables what is called the strict mode in JavaScript. When the ES5 updates to JavaScript were released in 2009, they added new features and modified some of the imperfect decisions previously made by the language creators.
To make sure that older versions of JavaScript keep working, these modifications are turned off by default and are only turned on using the ‘use strict’ directive.
‘use strict’ enables us to write more secure and cleaner JavaScript. Bad syntax or silent errors that were previously ignored will throw an error when in strict mode. For example, you cannot use an undeclared variable in strict mode.
a = 5 //X will not work
let a = 5 // will work
In summary, use strict mode:
- Prevents the occurrence of some JavaScript silent errors by changing them to throw errors.
- Fix mistakes that make it difficult for JavaScript engines to perform optimizations:
- Prevents usage of syntax likely to be defined in future versions of ECMAScript.
How to enable strict mode in JavaScript?
Strict mode is enabled by adding “use strict”; or ‘use strict’; to the beginning of a script file or a function/block.
Beginning of script file:
“use strict”;
//every other thing in the file
This means that all code in the script file will execute in strict mode.
Within a function or block:
function lookAtMe {
“use strict”;
//every other thing in the function
}
This means that every piece of code inside the function will operate in strict mode.
ES6 Javascript modules and classes have strict mode enabled by default, so you don’t need to declare ‘use strict’ when using those. Also, ‘use strict’ is supported by all modern browsers, except internet explorer 9 and lower.
Things not allowed in strict mode (non-exhaustive list)
- Disallows global variables. (Catches missing
var
declarations and typos in variable names) - Silent failing assignments will throw an error in strict mode (assigning
NaN = 5
😉 - Attempts to delete undeletable properties will throw (e.g
delete Object.prototype
) - Requires all property names in an object literal to be unique ( e.g
var x = {x1: "1", x1: "2"
} will throw an error) - Function parameter names must be unique (
function sum (x, x) {...}
will throw an error) - Forbids octal syntax (e.g
var x = 023
;.) - Forbids the
with
keyword eval
in strict mode does not introduce new variables- Forbids deleting plain names (e.g
delete x
😉 - Forbids binding or assignment of the names
eval
andarguments
in any form - Strict mode does not alias properties of the arguments object with the formal parameters. (e.g. in
function sum (a,b) { return arguments[0] + b
;} This works becausearguments[0]
is bound to a and so on. ) arguments.callee
is not supported
Blog Credits: Linda Ikechukwu
” Linda Ikechukwu is a Frontend Developer turned Technical Content Consultant. She enjoys strategizing on and creating content that helps tech startups connect with their target audience. “
Comments are closed.