JavaScript - What is strict mode?
Strict mode in JS means strict operating context. It can be enabled by putting ‘use strict’ at the top of the file or at the top of a function.
'use strict';
//other functions and variables
//To enable only for a particular function
function(param){
'use strict';
//Now only this function is in strict mode
}
Strict mode has the following effect on the code execution:
Variable can not be used without declaration.
=> {
(() 'use strict';
= 'someValue'; //Not allowed
undeclaredVar //Error: undeclaredVar is not defined
; })()
Not possible to use reserved keywords as variable names.
e.g. let
, const
, var
etc.
=> {
(() 'use strict';
var eval = 'someValue'; //Not allowed
//Unexpected strict mode reserved word
; })()
delete
of var
,
function
and function
arguments is not
allowed.
=> {
(() 'use strict';
var toBeDeleted = 'TryDeletingMe';
var aFunction = () => 'JustString';
delete toBeDeleted; // Not allowed
delete aFunction; // Not allowed
//Delete of an unqualified identifier in strict mode.
; })()
variables defined inside eval
are not available
outside of the eval
.
=> {
(() 'use strict';
eval('var varInEval = 12;');
console.log(varInEval);
//Error: varInEval is not defined
//In non-strict mode it would have printed 12
; })()
The default value of this
is undefined however
in non-strict mode it is window
object.
=> {
(() 'use strict';
console.log(this); //prints undefined
//In non-strict mode it prints global window object
; })()
Duplicating parameter in a function is not allowed
=> {
(() 'use strict';
//Not allowed
function withDuplicateParamName(param, param) {
console.log(param);
}//Duplicate parameter name not allowed in this context
; })()
It is always recommended to use strict mode.
Comments
Post a Comment