“use strict”; in JavaScript
Definition: The strict mode shows more warnings and makes JavaScript a cleaner language by applying some restriction on it.
How to enable strict mode :
1.Use top of the JS file or <script> tag
'use strict';
//rest of the code
2.Use top of the function block(Enable only for function)
function getData(){
'use strict';
//rest of the code}
Restriction on use strict mode
1. Undeclared variable is not allowed: All variables must be explicitly declared in strict mode. This helps to prevent typos.
function strictFunc() {
'use strict';
strictVar = 123;
}
strictFunc(); // ReferenceError: strictVar is not defined
2. Deleting an object is not allowed.
'use strict';let obj = {subject: 'JS'};
delete obj; // throws an error
3. Functions in Strict Mode
Functions must be declared at the top level of a scope:
function strictFunc() {
'use strict';
if (true) {
// SyntaxError:
function nested() {
}
}
}
Function declarations should not be placed in blocks. Use a function expression or move the statement to the top of the outer function.
function strictFunc() {
'use strict';
if (true) {
// OK:
var nested = function () {
};
}
}
Stricter rules for function parameters:
Function parameter names should be unique and using the same parameter name twice is forbidden. So in strict mode duplicate argument names are a syntax error.
"use strict";function hello(p1, p1) { console.log('hello')}; // !!! syntax errorhello();
The value of this
in non-methods functions is the global object. In case of non-strict mode.
function nonStrictFunc() {
console.log(this === window); // true
}nonStrictFunc();
If a strict mode function is executed using function invocation, its ‘this’ value will be undefined.
function strictFunc() {
'use strict';
console.log(this === undefined); // true
}strictFunc();
4.Setting Immutable Properties Fails with an Exception in Strict Mode
Illegal manipulations of properties throw exceptions in strict mode. For example, attempting to set the value of a read-only property throws an exception
var str = 'abc';
function nonStrictFunc() {
str.length = 7; // no effect, silent failure
console.log(str.length); // 3
}
function strictFunc() {
'use strict';
str.length = 7;
// TypeError: Cannot assign to read-only property 'length'
}
5.Unqualified Identifiers Can’t Be Deleted in Strict Mode
In non-strict mode, you can delete a global variable foo
like this:
delete foo
In strict mode, you get a syntax error whenever you try to delete unqualified identifiers. You can still delete global variables like this:
delete window.foo; // browsers
delete global.foo; // Node.js
delete this.foo; // everywhere (in global scope)
6. Assigning to a getter-only property is not allowed.
'use strict';let obj2 = { get x() { return 17; } };// assignment to a getter-only property
obj2.x = 5; TypeError: Cannot set property x of #<Object> which has only a getter"
7. Assigning to a new property on a non-extensible object is not allowed.
'use strict';let obj = {};
Object.preventExtensions(obj);// Assignment to a new property on a non-extensible object
obj.newValue = 'new value'; TypeError: Cannot add property newValue, object is not extensible"
8. The variable name arguments and eval are not allowed.
'use strict';let arguments = 'hello'; // throws an errorlet eval = 44;
You cannot also use these reserved keywords in strict mode.
implements
interface
let
package
private
protected
public
static
yield
9.Features That Are Forbidden in Strict Mode
Two more JavaScript features are forbidden in strict mode:
1.The with
statement is not allowed anymore (see The with Statement). You get a syntax error at compile time (when loading the code).
2. No more octal numbers: Outside strict mode, a number beginning with a 0
, such as 0644
, is interpreted as an octal number (0644 === 420
) For example:
console.log(010 === 8)
true
- In strict mode, you get a syntax error if you use this kind of literal:
function f() {
'use strict';
return 010
}SyntaxError: Octal literals are not allowed in strict mode.
In ECMAScript 2015, octal literals are supported by prefixing a number with “0o
"; for example:
var a = 0o10; // ES2015: Octal
3. In ECMAScript 5 strict-mode code, duplicate property names were considered a SyntaxError
.
'use strict';
var obj = { p: 1, p: 2 }; // syntax error prior to ECMAScript 2015
Benefits of Strict Mode
The use of strict mode:
This is handy when you are working with a legacy code base where switching on strict mode everywhere may break things.
- helps to write a cleaner code
- changes previously accepted silent errors (bad syntax) into real errors and throws an error message
- makes it easier to write “secure” JavaScript