Strict Mode in JavaScript
Strict Mode
Strict mode was introduced in ECMAScript 5 (ES5), which allows us to place a JavaScript program or function in a "strict" operating context using the "use strict" directive. Strict mode improves JavaScript code by enforcing better programming practices and eliminating various insecure and ill-advised features.
"use strict"; Why Strict Mode?
Enabling Strict Mode
The "use strict" directive can be enabled in two ways, shown below.
File Level:- To enable strict mode at file level, just add the "use strict" directive at the beginning of the script file, which applies to the entire script in a global context.
"use strict";pi = 3.14; // This will throw an error because pi is not declared Function Level:- To enable strict mode at function level, just add the "use strict" directive at the beginning of the function body, which applies to the entire function body.
function myFunction() { "use strict"; pi = 3.14; // This will throw an error because pi is not declared}myFunction(); General Restrictions in Strict Mode
Undeclared Variables are Not Allowed:- In strict mode, all the JavaScript variables must be declared, else it will throw an error (Reference Error).
"use strict";pi = 3.14; // This will throw an errormyObject = {name: 'pi', value:'3.14'}; // This will also throw an error Deleting a Variable or a Function is Not Allowed:- In JavaScript strict mode, we cannot delete a variable or a function, else it will throw an error (Syntax error).
"use strict";let pi = 3.14;function sayHello() { console.log("Hello");}delete pi; // This will throw an errordelete sayHello; // This will also throw an error Duplicate Parameter's are Not Allowed In JavaScript strict mode, if a function declaration has two or more parameters with the same name, then it will throw an error (Syntax Error).
"use strict";const number = 7;function addNumbers(number1, number2) { // This will throw an error return number1 * number2;}addNumbers(number, number); Octal Numbers and Octal Numbers with Escape Characters are Not Allowed In JavaScript strict mode, if we assign octal numbers (numbers prefixed with zero e.g. 011, 0357) with or without escape characters to a JavaScript variable, then it will throw an error (Syntax Error). However, ES6 support octal numbers by prefixing a number with 0o i.e. 0o10, 0o377, etc.
"use strict";let octNum = 010; // This will throw an errorlet octNum1 = "\010"; // This will also throw an error Writing to a Read-only Property is Not Allowed In JavaScript strict mode, if we assign value to a read-only property, then it will throw an error (Type Error).
"use strict";let person = {name: "Uttam", age: 28};Object.defineProperty(person, "gender", {value: "male", writable: false});person.gender = "female"; // This will throw an error Writing to a Get-only Property is Not Allowed In JavaScript strict mode, if we assign value to a get-only property, then it will throw an error (Type Error).
"use strict";let myObj = { get pi() { return 0 } };myObj.pi = 3.14; // This will throw an error Deleting an Undeletable Property is Not Allowed In JavaScript strict mode, if we delete a undeletable properties, then it will throw an error.
"use strict";delete Object.prototype; // This will throw an error