Last Updated : 03 Dec, 2024
In JavaScript, "use strict" is a directive that enables strict mode, which was first introduced in ECMAScript 5 (ES5) to assist in the writing of safer and more error-free code. "Use strict" is used to indicate that the code should be executed in strict mode, applying a stricter set of rules for JavaScript coding.
What is "use strict" in JavaScript?The "use strict" is the literal expression that enables strict mode for JavaScript. It is added at the top of a JavaScript file, function, or block, and tells the JavaScript engine to run the code in strict mode. In other words, this mode brings in a stricter set of rules for writing JavaScript with more warnings and throwing more errors.
Syntax: To enable strict mode, simply add "use strict"; at the top of a script or function:
"use strict"; console.log("Strict mode is enabled"); function myFunction() { "use strict"; let x = 3.14; console.log(x); }Why to use Strict Mode?
Strict mode was introduced to address some of JavaScript's problematic features and encourage developers to write safer, more optimized code. Here are some reasons why strict mode is beneficial:
"use strict"; x = 10; // Throws an error because 'x' is not declared
"use strict"; Object.defineProperty(this, "PI", { value: 3.14159, writable: false }); PI = 3.14; // Throws an error
"use strict"; function sum(a, a, c) { // Syntax error in strict mode return a + a + c; }
"use strict"; function showThis() {// Logs 'undefined' instead of the global object console.log(this); } showThis();
"use strict"; delete Object.prototype; // Throws an error in strict modeHow to use "use strict" Mode?
Strict mode can be enabled in two main ways: for the entire script or for individual functions.
"use strict"; // All code in this file runs in strict mode let name = "Sourav";
function display() { "use strict"; // Code inside this function runs in strict mode let message = "Hello, World!"; console.log(message); } // Code outside this function runs in normal mode1. Mistyping a Variable Name
In strict mode, assigning a value (3.14) to an undeclared variable (x) without prior declaration throws an error.
JavaScript
// Using a variable, without declaring it, is not allowed:
'use strict';
x = 3.14; // will throw an error
Output
Mistyping a Variable Name 2. Using a Variable Without Declaring ItIn strict mode, assigning a value to an undeclared variable (x) as an object ({p1:10, p2:20}) without prior declaration throws an error.
JavaScript
// Objects are variables too.
// Using an object, without declaring it, is not allowed:
'use strict';
// Will throw an error
x = { p1: 10, p2: 20 };
Output
Using a Variable Without Declaring It 3. Deleting a Variable or ObjectIn strict mode, attempting to delete a variable (delete x;) or function (delete x; where x is a function) declaration throws an error.
JavaScript
'use strict';
let x = 3.14;
// Deleting a function is also not allowed
'use strict';
function x(p1, p2) {};
// Will throw an error
delete x;
Output
Deleting a Variable or Object 4. Duplicating a Parameter NameUsing 'use strict';, defining a function with duplicate parameter names (p1, p1) causes an error due to parameter name duplication being disallowed in strict mode functions
JavaScript
'use strict';
// Will throw an error
function x(p1, p1) {};
Output
Duplicating a Parameter Name 5. Octal Numeric LiteralsHere we Using 'use strict';, 010 is interpreted as an invalid octal literal because octal literals (starting with 0) are not allowed in strict mode, causing an error when assigning to variable x.
JavaScript
'use strict';
// Will throw an error
let x = 010;
Output
Octal Numeric Literals 6. Escape CharactersHere we Using 'use strict';, \010 is interpreted as an invalid octal literal, causing an error when trying to assign it to variable x.
JavaScript
'use strict';
// Will throw an error
let x = \010;
Output
Escape Characters "use strict" Limitations and Compatibility ConsiderationsStrict mode is generally compatible with modern JavaScript, but there are some points to keep in mind:
In certain cases, enabling strict mode may not be appropriate:
Strict mode in JavaScript, introduced in ECMAScript version 5, enforces stricter parsing and error handling. It eliminates silent errors, enhances performance, disables unsafe features, and future-proofs code. Using "use strict" ensures more secure, optimized, and debuggable JavaScript code.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4