Table of Contents
The "use strict" directive was introduced in ECMAScript 5. It is not a statement, but a literal expression, ignored by earlier versions of JavaScript.
The purpose of "use strict" is to indicate that the code should be executed in strict mode. With strict mode, you cannot, for example, use undeclared variables.
Strict mode is declared by adding "use strict"; to the beginning of a script or a function:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strict Mode</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Declaring Strict Mode</h4>
<p id="demo"></p>
<script>
// Declare strict mode at the top of the script
"use strict";
// This works fine in strict mode (declared variable)
let carName = "Volvo";
document.getElementById("demo").innerHTML =
"carName: " + carName + "<br>" +
"Strict mode is active at script level";
</script>
</body>
</html>

Result View Example
Strict mode helps you write cleaner code by catching common coding mistakes and "unsafe" actions:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strict Mode</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Why Strict Mode?</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
// Without strict mode - undeclared variable is silently created
function nonStrict() {
undeclaredVar = 42; // No error (bad!)
}
nonStrict();
document.getElementById("demo1").innerHTML =
"Non-strict: undeclaredVar = " + undeclaredVar + " (silently created)";
// With strict mode - undeclared variable causes an error
function strictFunction() {
"use strict";
try {
undeclaredVar2 = 99; // Throws ReferenceError in strict mode
} catch(err) {
document.getElementById("demo2").innerHTML =
"Strict mode error: " + err.message;
}
}
strictFunction();
</script>
</body>
</html>

Result View Example
Strict mode prevents several unsafe actions:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strict Mode</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Not Allowed in Strict Mode</h4>
<p id="demo"></p>
<script>
"use strict";
let text = "<b>Not allowed in strict mode:</b><br><br>";
text += "1. Using undeclared variables (ReferenceError)<br>";
text += "2. Deleting variables (SyntaxError)<br>";
text += "3. Duplicating parameters (SyntaxError)<br>";
text += "4. Using reserved words as names<br>";
text += "5. Octal numeric literals<br>";
text += "6. Writing to read-only properties<br><br>";
// Example: writing to read-only property
try {
const obj = {};
Object.defineProperty(obj, "readonly", {value: 10, writable: false});
obj.readonly = 20; // TypeError in strict mode
text += "Read-only property write: no error (non-strict behavior)";
} catch(err) {
text += "Read-only property error: " + err.message;
}
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>

Result View Example
Strict mode only applies to the code where it is declared. If you declare it inside a function, it only applies to that function:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Strict Mode</h2>
<p>From w3schools.com, Experiment by Teeratus_R</p>
<h4>Watch Out!</h4>
<p id="demo1"></p>
<p id="demo2"></p>
<script>
// Strict mode is NOT active here (global scope without directive)
// Function with strict mode - only affects this function
function strictFunction() {
"use strict";
try {
undeclaredVar = 100; // Error in strict mode
} catch(err) {
document.getElementById("demo1").innerHTML =
"Inside strict function: " + err.message;
}
}
// Function WITHOUT strict mode - affected by global non-strict
function nonStrictFunction() {
undeclaredVar2 = 200; // No error (non-strict)
document.getElementById("demo2").innerHTML =
"Outside strict function (global): undeclaredVar2 = " + undeclaredVar2 + "<br>" +
"Strict mode only applies where it is declared";
}
strictFunction();
nonStrictFunction();
</script>
</body>
</html>

Result View Example
Document in project
You can Download PDF file.