Back to Blog Listing

Why You Should Use Strict Mode in JavaScript

JavaScript's strict mode, introduced in ECMAScript 5 (ES5), is a way to opt into a restricted variant of JavaScript. It makes debugging easier and prevents common errors. You enable it by typing "use strict"; at the top of a script or function.

Key Benefits of Strict Mode

1. Eliminates Silent Errors

In normal JavaScript, making mistakes like misspelling a variable name assigns it to the global object. Strict mode throws a ReferenceError instead.

"use strict";
val = 15; // Throws ReferenceError: val is not defined

2. Prevents Duplicate Parameters

Strict mode makes it syntax error to declare duplicate argument names in function signatures.

"use strict";
function sum(x, x, y) {} // SyntaxError: Duplicate parameter name not allowed

3. Secures the Value of "this"

Normally, if this is unspecified when executing a function, it defaults to the global window object. In strict mode, it is set to undefined, preventing accidental mutations to the global state.

4. Prohibits deleting variables and functions

Deleting declared variables, functions, or arguments is disallowed:

"use strict";
let x = 10;
delete x; // SyntaxError: Delete of an unqualified identifier in strict mode
💻

Try this code in our online compiler!

Don't just read about it. Write, run, and experiment with these JavaScript concepts instantly on our online workspace with autocomplete suggestions.

Share this tutorial:

CodeCompile