What is JavaScript Strict Mode and Why You Should Use It?

What is JavaScript Strict Mode and Why You Should Use It?

James Charlesworth

By James Charlesworth

3 December 20233 min read

JavaScript's "strict mode" is a feature that helps developers write safer and more reliable code. When enabled, it changes some JavaScript behaviors that are considered error-prone and can prevent common coding mistakes. Think of it as a way to make your JavaScript code more robust without the need for a full compilation step, like in TypeScript.

In this article, we'll dive deep into strict mode, explore its benefits, and look at some real-world examples.

Enabling Strict Mode

Activating strict mode is straightforward. All you need to do is include the string "use strict"; at the beginning of your JavaScript file or function.

"use strict";

This line instructs the JavaScript engine to interpret and execute the code in strict mode. Keep in mind that this directive is only recognized at the beginning of a script or a function, so you must decide to use strict mode for the entire script.

How Strict Mode Improves Your Code

  1. Undeclared Variables Without strict mode, if you use a variable that hasn't been declared, JavaScript will automatically create it as a global variable, which can cause unexpected behavior and bugs.

However, when strict mode is enabled:

x = 10;

This line will throw a ReferenceError stating that x is not defined.

  1. Disallowing the with Statement Strict mode prohibits the use of the with statement, considered bad practice. By doing so, it promotes clearer and more explicit code.

  2. Extra Function Arguments In regular JavaScript, if you call a function with more arguments than it is defined to accept, the extra arguments are simply disregarded.

For instance:

function exampleFunc(arg1, arg2){
// some code
}
exampleFunc(1, 2, 3);

The third argument in the function call above would be ignored in standard mode. However, with strict mode enabled, it throws a SyntaxError, pointing out potential coding errors.

  1. Writing to Read-Only Properties In non-strict mode, you can write to properties declared as writable: false. With strict mode, attempting such an action throws a TypeError.

  2. Duplicate Property Names In standard JavaScript, if an object has duplicate property names, the last one prevails. With strict mode turned on, it will raise an error if you try to create an object with repeated property names.

Conclusion

JavaScript's strict mode is a way to embrace a more constrained version of the language, eliminating many silent errors that can arise in standard JavaScript. By enabling strict mode, you can identify potential bugs earlier, ensuring your code remains consistent and easier to maintain. Whether you're a beginner or a seasoned developer, strict mode offers a pathway to write better, less error-prone code.

So, if you're new to JavaScript, kick off your journey with strict mode to avoid bad coding habits. And if you're a pro, consider revisiting your existing projects and switching to strict mode for enhanced robustness.

Watch on YouTube

Check out this video on our YouTube channel about strict mode.