User Tools

Site Tools


java-script:strict-mode

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
java-script:strict-mode [2023/07/25 20:03] – created odeftajava-script:strict-mode [2023/08/09 12:41] (current) odefta
Line 1: Line 1:
-====== Strict mode ======+====== Java Script Strict mode ====== 
 + 
 +===== Description =====
  
 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. 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.+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.\\ \\  
 +Strict mode makes several changes to the normal JavaScript behavior, such as turning some silent errors into thrown errors, fixing mistakes that can make it difficult for JavaScript engines to perform optimizations, and prohibiting certain syntax that's likely to be defined in future versions of ECMAScript. \\ \\  
 + 
 +It must be enclosed in quotes, either single (**'use strict'**) or double (**"use strict"**). 
 + 
 +<note tip> 
 +It's a good practice to use strict mode to catch common coding mistakes and "unsafe" actions like defining global variables. 
 +</note> 
 + 
 +===== Enabling strict mode ===== 
 + 
 +==== For an entire script ==== 
 + 
 +<code javascript> 
 +"use strict"; 
 + 
 +function sayHello() { 
 +  console.log(this.name, this.age); 
 +
 + 
 +// rest of the code 
 +</code> 
 + 
 +==== For a specific function ==== 
 + 
 +<code javascript> 
 +function sayHello() { 
 +  "use strict"; 
 +  console.log(this.name, this.age); 
 +
 + 
 +// rest of the code 
 + 
 +</code> 
 + 
 +===== Examples ===== 
 +==== This will not fallback to window in strict mode ==== 
 + 
 +<code javascript> 
 +"use strict"; 
 + 
 +function sayHello() { 
 +  console.log(this.name, this.age); 
 +
 + 
 +// ... rest of the code ... 
 + 
 +sayHello(); // TypeError: Cannot read properties of undefined (reading 'name'
 +</code> 
 +In strict mode, **this** inside the **sayHello** function will be **undefined** when called without an object, leading to a TypeError when attempting to access properties on it. 
 + 
 +==== Cannot use global variables defined without var ==== 
 + 
 +<code javascript> 
 +"use strict"; 
 +x = 3.14; // This will throw an error because x is not declared. 
 +</code> 
 + 
 +==== Denial of usage of special variables / properties ==== 
 + 
 +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. 
 + 
 +<code javascript> 
 +"use strict"; 
 +undefined = "test"; // This will throw an error 
 +</code> 
 + 
 +==== Prevent the removal of variables / functions ==== 
 + 
 +Deleting Variables, Functions, or Function Parameters: Attempting to delete variables, functions, or function parameters will throw an error. 
 + 
 +<code javascript> 
 +"use strict"; 
 +function x(){} 
 +delete x; // This will throw an error 
 +</code> 
 + 
 +==== Avoid duplicate parameter names ==== 
 + 
 +Duplicate Parameter Names: Duplicate parameter names in function declarations will throw an error. 
 + 
 +<code javascript> 
 +"use strict"; 
 +function x(p1, p1) {} // This will throw an error 
 +</code> 
 + 
 +==== Deprecated octal syntax ====
  
 +Octal Syntax: Octal syntax in ECMAScript 5 is deprecated, and strict mode will not allow it.
 +<code javascript>
 +"use strict";
 +var x = 010; // This will throw an error
 +</code>
java-script/strict-mode.1690304592.txt.gz · Last modified: 2023/07/25 20:03 by odefta