JavaScript

Last Updated: 2/2/2023

Strict Mode

  • Defines that JavaScript code should be executed in "strict mode".
  • Strict mode is declared by adding use strict to the beginning of a script or a function.
  • It helps you to write cleaner and secure code
  • Strict mode changes previously accepted "bad syntax" into real errors.
"use strict";

Not allowed

  • Using a variable, without declaring it, is not allowed:
  • Using an object, without declaring it, is not allowed:
  • Deleting a variable (or object) is not allowed.
  • Deleting a function is not allowed.
  • Duplicating a parameter name is not allowed:
  • Writing to a read-only property is not allowed:
  • Writing to a get-only property is not allowed:
  • Deleting an undeletable property is not allowed
"use strict";
        
// undeclared variable
v1 = 3.14;  //error


// delete variable
let v2 = 3.14;
delete v2;  //error     

// delete function
function f1(p1, p2) {};
delete f1;  //error

// assign to a readable property
const obj1 = {};
Object.defineProperty(obj1, "x", {
    value:0, writable:false
});
obj1.x = 3.14;  //error

// assign to get only property
const obj2 = {
    get x() {return 0} 
};
obj2.x = 3.14;  //error 

// Deleting an undeletable property is not allowed
delete Object.prototype;  //error