Understanding JavaScript Strict Mode

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 …


This content originally appeared on DEV Community and was authored by Accreditly

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!";

For an individual function:

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

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
  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
  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); 
  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();

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.


This content originally appeared on DEV Community and was authored by Accreditly


Print Share Comment Cite Upload Translate Updates
APA

Accreditly | Sciencx (2023-06-05T07:29:38+00:00) Understanding JavaScript Strict Mode. Retrieved from https://www.scien.cx/2023/06/05/understanding-javascript-strict-mode/

MLA
" » Understanding JavaScript Strict Mode." Accreditly | Sciencx - Monday June 5, 2023, https://www.scien.cx/2023/06/05/understanding-javascript-strict-mode/
HARVARD
Accreditly | Sciencx Monday June 5, 2023 » Understanding JavaScript Strict Mode., viewed ,<https://www.scien.cx/2023/06/05/understanding-javascript-strict-mode/>
VANCOUVER
Accreditly | Sciencx - » Understanding JavaScript Strict Mode. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/06/05/understanding-javascript-strict-mode/
CHICAGO
" » Understanding JavaScript Strict Mode." Accreditly | Sciencx - Accessed . https://www.scien.cx/2023/06/05/understanding-javascript-strict-mode/
IEEE
" » Understanding JavaScript Strict Mode." Accreditly | Sciencx [Online]. Available: https://www.scien.cx/2023/06/05/understanding-javascript-strict-mode/. [Accessed: ]
rf:citation
» Understanding JavaScript Strict Mode | Accreditly | Sciencx | https://www.scien.cx/2023/06/05/understanding-javascript-strict-mode/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.