DEV Community

Cover image for JavaScript Strict Mode Explained: A Beginner’s Guide to ‘use strict’
WISDOMUDO
WISDOMUDO

Posted on

JavaScript Strict Mode Explained: A Beginner’s Guide to ‘use strict’

Learn JavaScript Strict Mode in this beginner-friendly guide. Understand what use strict does, why it matters, and how to use it with examples.

Introduction

When you write JavaScript, it’s easy to make little errors that are missed. These errors might not break your code immediately, but they can cause problems later. To help developers avoid such issues, JavaScript introduced Strict Mode. By enabling Strict Mode, you write cleaner, safer, and more reliable code.

Strict Mode is activated by adding 'use strict' at the beginning of a script or a function. Once enabled, JavaScript enforces stricter rules, making it simpler to identify errors early.

What You’ll Learn

  • What Strict Mode is and why it exists.
  • How to enable Strict Mode in JavaScript.
  • Key differences between normal JavaScript and Strict Mode.
  • Common errors caught by Strict Mode.
  • Practical examples of using Strict Mode in your code.

What Is Strict Mode?

Strict Mode is a special mode in JavaScript that applies stricter rules to your code. With ECMAScript 5 (ES5), it was first introduced to make JavaScript more reliable by eliminating silent errors and enforcing better practices.

By default, JavaScript is quite forgiving. For example, you can accidentally create global variables without declaring them. Strict Mode prevents this and ensures your code is intentional.

Before learning how to use it, let’s explore how to make your scripts work in Strict Mode.

How to Enable Strict Mode

You can enable Strict Mode in two ways:

For the entire script, add 'use strict'; the topmost part of your JavaScript file.

'use strict';
let name = "Wisdom";

Enter fullscreen mode Exit fullscreen mode

For a specific function, add 'use strict'; inside a function to apply it only there.

function test() {
'use strict';
let age = 30;
}

Enter fullscreen mode Exit fullscreen mode

Once enabled, the code inside will follow strict rules. Now, let’s see what changes Strict Mode brings.

Key Features of Strict Mode

Strict Mode alters the behavior of JavaScript in several ways. Here are some of the most important:

Prevents Undeclared Variables

In normal mode, you can assign a value without declaring a variable:

x = 20;  // Creates a global variable automatically


Enter fullscreen mode Exit fullscreen mode

In Strict Mode, this throws an error:

'use strict';
x = 20; // Error: x is not defined

Enter fullscreen mode Exit fullscreen mode

Eliminates Silent Errors

Some actions in JavaScript fail silently. Strict Mode makes them throw errors, helping you spot problems quickly.

Restricts this in Functions

In normal mode, this inside a simple function refers to the global object. In Strict Mode, it becomes undefined, which prevents accidental global access.

Reserved Keywords Protection

Strict Mode reserves certain keywords (like implements, interface, package) for future use. Using them as variable names will throw errors.

Now that you’ve seen the rules, let’s put Strict Mode into real examples.

Examples of Strict Mode in Action

Example 1: Catching Undeclared Variables

'use strict';
myVar = 7; // Error: myVar is not defined
Enter fullscreen mode Exit fullscreen mode

Example 2: Preventing Duplicate Parameters

'use strict';
function add(a, a) { // Error: duplicate parameter name not allowed
return a + a;
}
Enter fullscreen mode Exit fullscreen mode

Example 3: Safer this Usage

'use strict';
function showThis() {
console.log(this); // undefined instead of global object
}
showThis();

Enter fullscreen mode Exit fullscreen mode

Through these examples, it’s clear that Strict Mode makes your code more predictable and secure.

Conclusion

JavaScript's Strict Mode is an effective technique that encourages good coding habits and prevents common mistakes. By enforcing stricter rules it helps you catch errors early, write cleaner code, and avoid bugs that can be difficult to trace.

For beginners, learning Strict Mode is essential because it sets the foundation for writing professional-level JavaScript. As you continue coding, making 'use strict' a regular part of your scripts will help you become a better, more reliable developer.

You can reach out to me via LinkedIn

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

It's actually not needed much these days. A lot of modern code lives inside ES modules - which are in strict mode by default, as is code inside classes.

Collapse
 
wisdomudo profile image
WISDOMUDO

You're absolutely right! With ES modules and classes now being in strict mode by default, a lot of the concerns around needing to explicitly add 'use strict' have been minimized. However, it's still valuable for beginners to understand how and why it was introduced, especially when working with older code or certain environments that don't automatically apply it. Plus, understanding it gives you a stronger grasp of JavaScript’s inner workings.