Strict mode in JavaScript: What you need to know
Introduction
JavaScript is a popular programming language used for developing web applications. It is a loosely typed language, which means that it does not require variable declaration before use. This feature can be helpful in some cases, but it can also lead to errors and make debugging difficult. Therefore, to address this issue, the concept of strict mode was introduced in JavaScript.
What is Strict Mode?
Strict mode is a way to introduce a new level of error-checking and code validation in JavaScript. When strict mode is enabled, the JavaScript interpreter will perform additional checks and raise errors in cases where it may have previously remained silent. These additional checks can help catch common coding mistakes and prevent errors from occurring in the first place.
Enabling Strict Mode
In order to enable strict mode in JavaScript, you need to add the "use strict" directive to the beginning of your code. This tells the browser to use strict mode for the entire file or function. For example:
"use strict";
function myFunction() {
// code goes here
}
Once strict mode has been enabled, the JavaScript interpreter will perform additional checks on your code to ensure that it meets the strict requirements. This includes catching common coding mistakes, such as using undeclared variables, assigning values to read-only properties, and using duplicate function parameter names.
Benefits of Strict Mode
Strict mode helps to ensure that your code is more secure and less error-prone. By catching common coding mistakes, the strict mode can help prevent errors from occurring in the first place, which can save you time and effort in the long run. Some of the benefits of the strict mode include:
Catching common coding mistakes, such as using undeclared variables and assigning values to read-only properties
Disabling some features that are considered harmful or unnecessary, such as the statement and the eval() function
Providing better error messages to help you debug your code more easily
Making your code more future-proof by preventing you from using features that may be removed or changed in future versions of JavaScript.
Examples of strict mode in action
1. Avoiding undeclared variables
One of the most common issues in JavaScript is the use of undeclared variables. In non-strict mode, these variables would simply be created as global variables, which can cause issues with scope and naming collisions. In strict mode, however, using an undeclared variable will cause an error to be thrown.
'use strict';
x = 5; // Throws an error: "Uncaught ReferenceError: x is not defined"
2. Preventing the use of reserved words as variable names
In non-strict mode, it's possible to use reserved words (such as let
and class
) as variable names. This can lead to confusing and hard-to-debug code. In strict mode, however, trying to use a reserved word as a variable name will throw an error.
'use strict';
let let = 5; // Throws an error: "Uncaught SyntaxError: Unexpected token 'let'"
3. Disallowing duplicate properties in objects
In non-strict mode, it's possible to create objects with duplicate property names. This can lead to unexpected behavior and difficult-to-debug issues. In strict mode, however, creating objects with duplicate property names will throw an error.
'use strict';
const obj = {
prop1: 'foo',
prop1: 'bar', // Throws an error: "Uncaught SyntaxError: Duplicate data property in object literal not allowed in strict mode"
};
Limitations of Strict Mode
While strict mode can be very beneficial, it does have some limitations. One of these is that it can break existing code that relies on certain features that are disabled in strict mode. Additionally, some third-party libraries may not work properly with strict mode enabled, so it is important to test your code thoroughly before enabling it.
Another limitation of strict mode is that it is not supported in older browsers, such as Internet Explorer 9 and below. This means that if you need to support older browsers, you may not be able to use strict mode in your code.
Conclusion
Strict mode is an important feature in JavaScript that can help you write more secure and error-free code. By enabling strict mode, you can catch common coding mistakes and disable features that are considered harmful or unnecessary. However, it is important to be aware of its limitations and to test your code thoroughly before enabling it. If you are working with older browsers, you may not be able to use strict mode in your code, but for newer browsers, a strict mode is a powerful tool that can help you write better JavaScript code.
Subscribe to my newsletter
Read articles from Akshat Vishwakarma directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Akshat Vishwakarma
Akshat Vishwakarma
I am Akshat Vishwakarma from India and as a front-end software developer, I am passionate about creating visually appealing and user-friendly interfaces for web and mobile applications. With a strong background in Javascript, I have experience working with React, Next.js, Redux, and Redux-Saga, as well as no-code platforms like Webflow.