'use strict'
Enables strict mode: errors that used to be silent become explicit.
Available since: ES5
Syntax
'use strict';Examples
Assigning an undeclared variable is an error:
'use strict';
try {
undeclared = 5;
} catch (error) {
console.log(error.name);
}Output:
ReferenceErrorNotes
The string must come first in a file or function. Modules (import/export) and classes are always strict, so modern code rarely needs it.
See also
- Object.freeze — Prevents any changes to an object.
- var — The legacy way to declare a variable.
In the tutorial
- Strict mode — The use strict directive in JavaScript: which mistakes strict mode catches, this in functions, forbidden syntax and where strict mode is turned on automatically.