Open In App

What are the Benefits of Using the use strict Directive in JavaScript ?

The "use strict" directive enables strict mode in JavaScript, which provides several benefits:

Example: Here, the code is in strict mode due to the presence of the "use strict" directive at the beginning. When the variable x is assigned a value without being declared first, a ReferenceError is thrown, indicating that x is not defined. Strict mode helps catch such errors at runtime, making the code more robust.




"use strict";
 
x = 10; // Throws a ReferenceError: x is not defined

Output:

// Throws a ReferenceError: x is not defined
Article Tags :