Understanding JavaScript Strict Mode

Accreditly - Jun 5 '23 - - Dev Community

In JavaScript, the language provides a feature known as 'strict mode', introduced in ECMAScript 5 (ES5), that helps developers avoid common JavaScript pitfalls. In this article, we will dive into what strict mode is, how to enable it, and the benefits it provides.

For a more in-depth article you can check out the counter-part to this article over on our website: Understanding JavaScript Strict Mode on Accreditly.

What is Strict Mode?

Strict mode is a way to opt into a restricted variant of JavaScript. In strict mode, JavaScript eliminates some JavaScript silent errors by changing them to throw errors. It fixes mistakes that make it difficult for JavaScript engines to perform optimizations, and it prohibits some syntax likely to be defined in future versions of ECMAScript.

Enabling Strict Mode

To enable strict mode in JavaScript, you use the string "use strict". This can be done for an entire script or within an individual function.

For an entire script:

"use strict";
var v = "Hello, I'm in strict mode!";
Enter fullscreen mode Exit fullscreen mode

For an individual function:

function strictFunc() {
  "use strict";
  var v = "Hello, I'm in strict mode inside a function!";
}
Enter fullscreen mode Exit fullscreen mode

The "use strict" directive is only recognized at the beginning of a script or a function.

The Benefits of Using Strict Mode

Strict mode helps out in a couple of ways:

  1. It catches common coding mistakes and "unsafe" actions.

For instance variables must be declared with var, let, or const. A variable that has not been declared will cause an error.

"use strict";
x = 3.14; // This will cause an error because x is not declared
Enter fullscreen mode Exit fullscreen mode
  1. It prevents the use of future ECMAScript reserved words. For example:
"use strict";
var let = "Hello"; // This will cause an error because "let" is a reserved word in ES6
Enter fullscreen mode Exit fullscreen mode
  1. It simplifies eval() and arguments.

In strict mode, variables declared within an eval() statement will not create variables in the surrounding scope.

"use strict";
eval("var x = 10;");
// This will cause an error because x is not defined outside the scope of eval()
console.log(x); 
Enter fullscreen mode Exit fullscreen mode
  1. It restricts this to undefined for functions that are not methods or constructors.

In non-strict mode, this will default to the global object, window in a browser context.

"use strict";
function myFunction() {
  console.log(this); // Will output: undefined
}
myFunction();
Enter fullscreen mode Exit fullscreen mode

Using strict mode can help you catch errors that would otherwise have been silently ignored. It also helps prevent you from using potentially problematic syntax and making inefficient coding decisions. Strict mode can make your JavaScript code more robust and maintainable, and it's a best practice to start your scripts with the "use strict" directive.

. . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player