JavaScript · JS Tutorial · Lesson 39
JS Use Strict
Write safer JavaScript with "use strict".
"use strict"; makes JavaScript execute in "strict mode", which turns some silent mistakes into errors.
Example
<p id="demo"></p>
<script>
"use strict";
let msg = "";
try {
undeclared = 3.14; // not allowed in strict mode
} catch (e) {
msg = e.name + ": " + e.message;
}
document.getElementById("demo").innerHTML = msg;
</script>
- Using an undeclared variable is not allowed
- Deleting a variable or function is not allowed
- Duplicate parameter names are not allowed
- In functions,
thisisundefinedinstead of the global object - Modules and classes are automatically in strict mode
Keep reading “JS Use Strict”
Sign up to read the full lesson, try every example in the live editor, and take the 5-question quiz.