What does 'use strict;' means in Javascript

By FoxLearn 2/27/2025 3:19:38 AM   14
"Use strict;" is a directive in JavaScript that enforces stricter parsing and error handling on the code. When you apply strict mode, the interpreter enforces stricter rules, catching common coding mistakes and preventing certain problematic or error-prone syntax.

How Strict Mode Works:

Strict mode changes the way JavaScript works by eliminating some silent errors and turning them into errors that are easier to debug. It also helps JavaScript engines optimize performance, as strict mode code can be executed faster than non-strict code. Additionally, it prevents the use of features that may be reserved in future versions of ECMAScript (e.g., the use of class).

Strict mode can be applied to the entire script or specific functions. It does not apply to block-level statements like if or for. For example, applying strict mode within these blocks has no effect. Essentially, strict mode adds more rigorous error-checking to your JavaScript code.

How to Enable Strict Mode:

You can enable strict mode by including the following string at the top of your script or function:

1. In a Script Tag or External File:

<script>
"use strict";
// Your strict code goes here
</script>

2. In a Function (Anonymous or Named):

(function() {
   "use strict";
   // All code within this function will run in strict mode
})();

function myAction() {
   "use strict";
   // Strict mode is enabled here too
}

Using this in Strict Mode

Strict mode changes how the this keyword behaves. In normal JavaScript, this can reference the global object in some situations, which can lead to unexpected results. However, in strict mode, this will remain undefined if it's used incorrectly.

Here’s an example that shows the behavior of this in strict mode:

"use strict";

function checkThis() {
   console.log(this);
}

checkThis(); // Throws "undefined" in strict mode

Without strict mode, this would log the global object (window in browsers). With strict mode enabled, it logs undefined, ensuring that this is only used in a valid context.

Other Key Restrictions in Strict Mode:

1. Prevents Duplicate Property Names:

var myObject = {
  hello: "World",
  hello: "Universe"
};

In strict mode, the code will throw an error: Uncaught SyntaxError: Duplicate data property in object literal not allowed in strict mode.

2. Prevents Use of with Statement: Strict mode disallows the use of the with statement, which can make code harder to understand:

"use strict";
with (Math) {
  var x = cos(PI);
}

This will throw an error: Uncaught SyntaxError: Strict mode code may not include a with statement.

3. Prevents Implicit Global Variables: Strict mode prevents the creation of global variables without an explicit declaration:

"use strict";
function test() {
  undeclaredVar = 10;
}
test(); // Throws Uncaught ReferenceError: undeclaredVar is not defined

4. Forbids Reserved Keywords: Using certain keywords as variable names is forbidden in strict mode, such as:

"use strict";
var interface = "example"; // SyntaxError: Unexpected strict mode reserved word

5. Disallows eval-Defined Variables: In strict mode, any variable declared inside eval() is not visible outside of it:

eval("var testVar = 5;");
console.log(testVar); // ReferenceError: testVar is not defined

6. Prevents Octal Syntax: Octal numbers (numbers starting with a leading zero) are not allowed in strict mode:

"use strict";
var num = 012; // SyntaxError: Octal literals are not allowed in strict mode

7. Prevents Modifying Non-Extensible Objects: Trying to add properties to a non-extensible object will result in an error:

"use strict";
var obj = {};
Object.preventExtensions(obj);
obj.newProp = "value"; // Throws: Cannot create property 'newProp' on a non-extensible object

Strict mode is supported by all major modern browsers (except for Internet Explorer 9 and earlier). If you're concerned about browser compatibility, you can check resources like "Can I use" to confirm support.