Trivia Questions1 min readPremium

What does “use strict” do?

The Impact of “use strict” in JavaScript

The “use strict” directive in JavaScript triggers strict mode, a feature that enforces more rigorous parsing and error handling in your code, leading to improved performance and reliability. MDN’s strict mode reference lists every rule.

Enhanced Error Checking

Strict mode raises errors for common coding bloopers, such as using undeclared variables, which in non-strict mode would fail silently. This immediate feedback helps developers catch and correct issues early in the development process.

// Without "use strict"
x = 23; // Accidentally creates a global variable

// With "use strict"
"use strict";
x = 23; // ReferenceError: x is not defined

Disallowed Features

Strict mode restricts certain syntax and features considered unsafe or error-prone, like the use of with statements, setting properties on primitive values, and more. It aims to make JavaScript more secure and easier to debug.

Securing “this” Keyword

In strict mode, the this keyword in functions invoked in the global context is undefined, reducing the risk of inadvertently modifying the global object.

"use strict";
function myFunction() {
  return this; // returns undefined in strict mode
}

Conclusion: When to Use Strict Mode

Employing “use strict” in your JavaScript projects can lead to cleaner, safer, and... premium content.

Premium

Keep reading with Premium

Get the rest of this guide plus every premium question, solution, and system design deep dive.

  • All premium solutions
  • System design deep dives
  • Company-specific prep

4.9/5 from our users