Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials


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.

syntax
"use strict";

Why Strict Mode?

  • Strict mode helps us write more secure JavaScript code.
  • Strict mode converts previously accepted bad syntax into real errors.
  • In strict mode, any assignment to a non-writable property, a get-only property, a non-existing property/variable/object, will throw an error.

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.

example
"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.

example
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).

example
"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).

example
"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).

example
"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.

example
"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).

example
"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).

example
"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.

example
"use strict";delete Object.prototype; // This will throw an error

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.