JavaScript use strict

Subject: JavaScript

"use strict" in JavaScript enables strict mode which makes the code more secure, optimized, and easier to debug by enforcing stricter parsing and error checks.


Why Use "use strict"?

  • Prevents use of undeclared variables.
  • Disallows duplicate parameter names.
  • Throws errors on silent failures.
  • Disables problematic or deprecated features.
  • Ensures better compatibility with future JavaScript versions.

How to Use Strict Mode

1. At the Top of a Script

2. Inside a Function


Differences Between Strict and Non-Strict Mode


Examples

Example 1: Undeclared Variable

Example 2: Duplicate Parameters

Example 3: this in a Function

Example 4: Writing to Read-Only Property


Key Takeaways

  • "use strict" enforces stricter rules and better error detection.
  • Helps prevent common bugs by catching silent errors.
  • Can be applied globally or locally within functions.
  • Recommended for writing cleaner, safer, and future-proof JavaScript code.
  • Changes behavior of variables and functions to be more consistent.
Next : JS Classes