Strict mode in JavaScript programming
Strict mode in JavaScript
Strict mode in JavaScript is a way to enforce stricter rules and restrictions when writing JavaScript code, which can help identify and prevent potential errors, security leaks, or performance issues. It is enabled by adding the following line at the beginning of a script or a function:
"use strict";
var
, let
, or const
before being used, otherwise an error is thrown.
Non-strict mode:
x = 10; // No error, even though 'x' is not declared
console.log(x); // 10
"use strict";
x = 10; // Error: x is not defined
console.log(x);
NaN = "not a number"; // No error, even though NaN is read-only
console.log(NaN); // "not a number"
"use strict";
NaN = "not a number"; // Error: Assignment to constant variable
console.log(NaN);
var num = 010; // No error, 'num' is treated as an octal number (8 in decimal)
console.log(num); // 8
"use strict";
var num = 010; // Error: Octal literals are not allowed in strict mode
console.log(num);
Comments
Post a Comment