Strict Mode (JavaScript)
Strict mode is an opt-in mode that makes JavaScript catch more mistakes by turning silent errors into thrown errors. You enable it with the line "use strict".
JavaScript
Adding "use strict"; at the top of a file or function enables stricter rules — for example, assigning to an undeclared variable throws instead of creating a global.
ES modules and class bodies run in strict mode automatically, so you often get it without writing the directive.
Syntax
"use strict";Examples
JavaScript
"use strict";
function test() {
// x = 5; would throw ReferenceError
let x = 5;
return x;
}
console.log(test());Output:
5