Strict mode in JavaScript
Strict mode in JavaScript is a special operating mode that helps write safer and better quality code. By enabling it, you activate additional checks and restrictions that prevent common errors.
What Does Strict Mode Change?
- Prohibits using undeclared variables.
bash
"use strict";
x = 10; // Error: variable not declared- Prohibits deleting variables, functions or objects.
bash
"use strict";
delete x; // Error: cannot delete variable or function- Restricts
thisin functions. Without strict mode:
bash
function showThis() {
console.log(this); // `this` refers to global object (window in browser)
}With strict mode:
bash
"use strict";
function showThis() {
console.log(this); // `this` will be undefined
}- Prevents using reserved words.
Words like
implements,interface,packagecannot be used as variables or function names.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.