What is strict mode in JavaScript and why should you use it?
Photo: Jadson Thomas/Pexels
Introduction
Like any evolving technology, JavaScript has undergone significant changes over time. Early versions of JavaScript, while groundbreaking for their time, had some quirks and inconsistencies. As language matured, designers recognized these flaws and sought to correct them. However, a crucial challenge arose: maintaining backward compatibility. Breaking existing code to fix these issues wasn't an option.
The solution? Strict Mode. Introduced in ECMAScript 5 (ES5), strict mode is a way to opt into a cleaner, more robust version of JavaScript. Many of the historical quirks are disabled in the strict mode, encouraging better coding practices and preventing standard errors.
Table of Contents
- How to use strict mode
- Strict mode and modern JavaScript (ES6+)
- Why strict mode matters
How to use strict mode
To activate the strict mode, add the use strict
directive at the beginning of your JavaScript file or
within a function.
Strict mode and modern JavaScript (ES6+)
With the introduction of ES6 (ECMAScript 2015) and later versions, many new language features implicitly invoke strict mode. For instance, when you use the class keyword to define a class or create an ES6 module, all the code within that class or module is automatically in strict mode. This means that older, less strict behaviors are unavailable in these modern contexts.
Why strict mode matters
Relying on some of JavaScript's older, less strict behaviors can lead to unexpected bugs and headaches down the line. Strict mode helps you build a more solid foundation for your code by:
Catching common coding mistakes
Strict mode converts silent errors into actual errors, providing immediate feedback and helping you identify potential problems early in development.
Accidental global variables
Accidental global variables occur when you assign a value to a variable that hasn't explicitly declared using
var,
let,
or const.
JavaScript will automatically create a global variable with that name in non-strict
mode.
In strict mode, if you try to assign a value to an undeclared variable, JavaScript will throw a ReferenceError,
preventing the creation of the global variable and alerting you to the problem.
Assignment to a Read-Only Property
In non-strict mode, it will silently fail if you try to assign a new value to a read-only property. There's no error; the assignment doesn't happen. This can be confusing because your code might appear to be working correctly, but the property remains unchanged.
Strict mode addresses this issue by throwing a TypeError
when you attempt to assign to a read-only property.
This makes the error explicit, helping you identify and fix the problem quickly.
Preventing bad syntax
Strict mode disallows certain syntax that is considered poor practice or could lead to performance issues, encouraging you to write cleaner, more maintainable code.
Duplicate parameter names
Older versions of JavaScript (before strict mode) allowed you to have duplicate parameter names within a function definition. Having duplicate parameter names can lead to confusion and unexpected behavior. When you call the function with multiple arguments, it becomes unclear which argument corresponds to which parameter. This makes your code harder to understand, debug, and maintain, especially in complex functions.
Strict mode in JavaScript helps prevent this issue by throwing a SyntaxError
if you try to define a function
with duplicate parameter names. This enforces cleaner code and helps you avoid potential errors.
Octal Literals
In older versions of JavaScript (pre-strict mode), octal literals were allowed. They were denoted by a
leading zero (0) followed by a sequence of octal digits (0-7). For example, 010
in octal represents the
decimal number 8.
The use of octal literals can lead to confusion and errors, especially for developers who are not familiar
with octal notation. It's easy to accidentally create an octal literal when you intended to write a decimal
literal. For instance, if you write 012
intending it to be the decimal number 12, it will actually be
interpreted as the decimal number 10 (because 12 in octal is 10 in decimal).
Strict mode in JavaScript disallows the use of octal literals. If you try to use an octal literal in strict
mode, it will throw a SyntaxError
, preventing potential errors and confusion.
Disabling fetures prone to errors
Strict mode in JavaScript helps improve the language by disabling certain features that are known to be problematic or prone to errors. These features might have been part of earlier JavaScript specifications but have since been identified as potentially confusing or harmful to code quality.
with
statement
The The with
statement was intended to provide a shorthand for accessing properties of an object. It created a
new scope where the properties of the specified object were accessible without needing to repeatedly
reference the object.
The with
statement can make code harder to read and understand because it can be unclear where variables
are coming from (the with
block's scope or the surrounding scope). This ambiguity can lead to confusion
and unexpected behavior.
Strict mode completely disallows the with statement, forcing you to write clearer code by explicitly referencing the objects you're working with.
Deleting a variable, function, or function argument
In non-strict mode, you can delete variables, functions, and function parameter without any errors. This behavior can lead to unexpected results and make your code harder to maintain.
Strict mode helps prevent this issue by throwing a SyntaxError
if you try to use the delete operator on a
variable, function or function parameter. This enforces cleaner code and helps you avoid potential errors.
Making code more secure
Strict mode in JavaScript also enhances security by disallowing certain actions that could potentially be exploited by malicious actors. By preventing these actions, strict mode helps protect your code and data from vulnerabilities.
eval()
and arguments
as variable names
In non-strict mode, you can use eval
and arguments
as variable names without any issues. This can be
problematic because eval
is a function that executes JavaScript code represented as a string, and
arguments
is an array-like object that holds the arguments passed to a function.
Strict mode in JavaScript prevents you from using eval
and arguments
as variable names, reducing the
risk of accidental misuse and potential security vulnerabilities.
eval()
Restricting The eval()
function in JavaScript is a powerful but potentially dangerous feature. It allows you to execute
arbitrary code represented as a string. While this can be useful in certain situations, it can also pose
security risks and make your code harder to maintain and debug.
In non-strict mode, when you use eval()
, it can modify the scope of the surrounding code. This means that
variables and functions declared inside the eval() code can affect variables and functions outside of it,
potentially leading to unexpected behavior and vulnerabilities.
Strict mode in JavaScript restricts the use of eval()
by creating a new scope for the code executed by
eval()
. This means that variables and functions declared inside the eval()
code are not accessible
outside of it, reducing the risk of unintended side effects and security vulnerabilities.
this
in Functions (preventing accidental global this)
In non-strict mode, if a function is called without an explicit this
context, the this
value defaults to
the global object (window
in browsers, global
in Node.js). This can lead to accidental global this
binding, where the function unintentionally modifies global variables or properties.
Strict mode in JavaScript prevents this issue by setting the default this
value to undefined
when a
function is called without an explicit this
context. This helps avoid accidental global this
binding
and makes your code more predictable and secure.