JavaScript Strict Mode

JavaScript’s "use strict" directive enables a stricter set of rules for your code, helping catch common bugs and preventing unsafe actions.

It’s a best practice to use strict mode in modern JavaScript to write cleaner, more secure, and error-free code.



What is Strict Mode?

Strict mode is a special way to run JavaScript. It helps you write safer and cleaner code.

When you turn on strict mode, JavaScript will show you more errors if you make mistakes — like using a variable without declaring it first.

To use strict mode, just add "use strict"; at the very top of your JavaScript file or inside a function.

javascript
"use strict";
// your code here

You can use it in two ways:

  • Globally: Put it at the top of your file to apply it everywhere.
  • Locally: Put it inside a function to apply it only there.
javascript
function myFunction() {
  "use strict";
  // strict mode is only active inside this function
}

Using strict mode is a good habit. It can help you avoid bugs and write better code.


Example 1: Catching Mistakes with Strict Mode

In strict mode, if you try to use a variable without declaring it first, JavaScript will show an error. This helps you find mistakes early.

javascript
"use strict";

x = 10; // Error: 'x' is not declared

console.log(x);

How It Works:

  • The code uses "use strict"; to enable strict mode.
  • We try to assign a value to x without declaring it first with let, const, or var.
  • Strict mode stops this and shows an error, helping you catch bugs.

Output:

ReferenceError: x is not defined

Example 2: Preventing Duplicate Variable Names

Strict mode helps you avoid declaring the same variable name twice in the same scope. This keeps your code clear and error-free.

javascript
"use strict";

let age = 25;
let age = 30; // Error: Duplicate declaration of 'age'

console.log(age);

How It Works:

  • We declare a variable called age and set it to 25.
  • Trying to declare age again causes an error in strict mode.
  • This stops confusion and bugs caused by reusing variable names by mistake.

Output:

SyntaxError: Identifier 'age' has already been declared

Common Errors in Strict Mode

Strict mode helps catch common mistakes by throwing errors in these situations:

  • Using a variable without declaring it first.
    x = 5; will cause an error if x is not declared.
  • Declaring the same variable twice in the same scope.
    Example: let age = 20; followed by let age = 30; causes an error.
  • Deleting variables, functions, or arguments.
    Example: delete x; will throw an error.
  • Writing to read-only properties.
    For example, trying to change a constant or a property that cannot be changed.
  • Using duplicate parameter names in functions.
    Example: function sum(a, a) will cause an error.
  • Using the with statement.
    This is forbidden in strict mode because it makes code harder to understand.

Using strict mode helps you write safer code by stopping these common mistakes.


Frequently Asked Questions

What is JavaScript strict mode?

JavaScript strict mode is a way to opt in to a restricted variant of JavaScript by adding 'use strict' at the beginning of a script or function, which helps catch common coding errors and unsafe actions.


How do I enable strict mode in JavaScript?

You enable strict mode by placing the statement 'use strict'; at the top of a JavaScript file or function before any other code.


What are some common errors caught by strict mode?

Strict mode catches errors such as assigning values to undeclared variables, deleting undeletable properties, duplicate parameter names in functions, and using reserved keywords as variable names.


Does strict mode affect the performance of JavaScript?

Strict mode can sometimes improve performance because it enables optimizations in JavaScript engines, but its primary purpose is to enforce better coding practices and prevent bugs.


Can I use strict mode in older browsers?

Most modern browsers support strict mode. However, very old browsers might not recognize 'use strict' and will ignore it, so it's generally safe to use today.



What's Next?

Now that you've explored strict mode, next you’ll dive into JavaScript regular expressions (regex) — powerful tools for pattern matching and text processing.