JS Tutorial



JS STRICT MODE


JavaScript Strict Mode ๐Ÿ”

Strict Mode is a way to opt in to a restricted variant of JavaScript. It helps you write cleaner code and catch common bugs earlier.

๐Ÿ”ง Enabling Strict Mode

To enable strict mode, add "use strict"; at the beginning of a script or a function:

"use strict";
x = 10; // โŒ Error: x is not defined

Without strict mode, this would not throw an error:

x = 10; // โœ… Allowed in non-strict mode (but bad practice)

๐Ÿ“‹ Benefits of Strict Mode

  • Prevents usage of undeclared variables
  • Makes assignments to read-only properties throw errors
  • Disallows duplicate parameter names
  • Helps avoid using reserved keywords

๐Ÿ’ก Example in Function Scope

function demo() {
  "use strict";
  let y = 20;
  // z = 30; // โŒ Error: z is not defined
}

๐Ÿงช Try It Yourself

Tip: Always use strict mode in your scripts for safer and cleaner code!

๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review