User Tools

Site Tools


java-script:strict-mode

This is an old revision of the document!


Java Script Strict mode

The “use strict” directive in JavaScript was introduced in ES5 to help you catch common coding mistakes and “unsafe” actions like assigning values to read-only variables or using some of the reserved keywords.

When you put “use strict” at the top of your JavaScript code or function, the JavaScript interpreter will switch to the Strict Mode, and it will start to catch and throw errors for actions that would be ignored or would fail silently in normal, non-strict code.

Examples:

"use strict";
x = 3.14; // This will throw an error because x is not declared.

Assigning to Read-Only Global Variables and Properties: Assigning a value to a read-only property, a getter-only property, a non-writable global variable, or a non-writable property of an object will throw an error.

"use strict";
undefined = "test"; // This will throw an error

Deleting Variables, Functions, or Function Parameters: Attempting to delete variables, functions, or function parameters will throw an error.

"use strict";
function x(){}
delete x; // This will throw an error

Duplicate Parameter Names: Duplicate parameter names in function declarations will throw an error.

"use strict";
function x(p1, p1) {} // This will throw an error

Octal Syntax: Octal syntax in ECMAScript 5 is deprecated, and strict mode will not allow it.

"use strict";
var x = 010; // This will throw an error
java-script/strict-mode.1690381327.txt.gz · Last modified: 2023/07/26 17:22 by odefta