use strict in JavaScript
Overview 'use strict' enables JavaScript strict mode. This line must be placed at the very beginning of the script, otherwise strict mode will not work. In strict mode, the interpreter will explicitly throw errors on actions that it previously skipped. Once strict mode has been enabled, it cannot be disabled for a file. 'use strict' // Place the line at the very beginning of the file. // Another code... Strict mode was introduced to JavaScript with the ECMAScript 5 standard in 2009. This standard added many new features to the language, but in order to keep older browsers running, a special strict execution mode was introduced that included new language features. A rigorous regime does the following: Throws errors when some unsafe constructs are used in the code. Turns off language features that confuse the code and therefore should not be used. Prevents the use of words that may be used as keywords in the future. use strict Restrictions You cannot use variables without a declaration The strict mode interpreter will throw an error if you access a variable without declaring it: 'use strict' const name = 'Alice' console.log(name)// Alice age = 18 console.log(age) // Uncaught ReferenceError: age is not defined Without strict mode, the interpreter will then create the variable age in the global scope. If you execute the code from the example in the browser console without 'use strict', everything will be executed without errors, and the age field with the value 18 will be written to the global window object. Clear error if the field value cannot be changed or deleted Using the Object.defineProperty() or Object.preventExtensions() methods in JavaScript, you can prevent object fields from being overwritten. If strict mode is enabled, an attempt to overwrite a field will result in an error. 'use strict' const obj = {} Object.defineProperty(obj, 'newProp', { value: 'Alice', writable: false }) console.log(obj.newProp) // Alice obj.newProp = 'James' // Uncaught TypeError: Cannot assign to read only property 'newProp' of object #. 'use strict' const notExtensableObj = {} Object.preventExtensions(notExtensableObj) notExtensableObj.newProp = 'Value' // Uncaught TypeError: Can't add property newProp, object is not extensible. An error will be thrown in strict mode and when attempting to remove a field from an object when it cannot be done. const obj = {} Object.defineProperty(obj, 'newProp', { value: 'Anna', configurable: false }) delete obj.newProp // Uncaught TypeError: Cannot delete property 'newProp' of #. If you run all the code samples above without strict mode, they will execute without errors, but the field values will not change. Function parameters cannot have the same names If you declare two parameters with the same name in a function, strict mode will throw a runtime error. 'use strict' // Uncaught SyntaxError: Duplicate parameter name not allowed in this context: function sum(a, b, a) { // ... } Without use strict, the interpreter will execute the code without errors, but it will be impossible to access the overridden parameter. function sum(a, b, a) { console.log(a) console.log(b) } sum(1, 2, 3) // Prints 3 and 2, the first argument is lost forever. Other behaviour this With strict mode enabled, this will no longer default to referring to a global object. 'use strict' function logThis() { console.log(this) } logThis() // undefined Without use strict, if you call a function in a global context (e.g. in a browser console), this will always reference the global object. It is forbidden to use reserved words In strict mode it is forbidden to use in code some words that have been specially reserved to be used in the future. These are the words implements, interface, let, package, private, protected, public, static, yield. Some of these words are already used at the moment: for example, declaring variables via let or returning a value from a generator via yield. Restriction of unsafe designs Additionally, enabling strict mode prevents you from using the with construct in your code and clears variables created with eval(). Both of these constructs are deprecated and potentially dangerous, so they are not used in modern JavaScript. How to Spell All modern JavaScript builders know how to add 'use strict' to files themselves. That's why nobody usually declares strict mode explicitly in code at the moment. And with the advent of JavaScript modules, they all run in strict mode by default. 'use strict' is called a directive because it is not just a string, but a special instruction to the interpreter. Therefore, it is very important to put the strict declaration at the very beginning of the script, so that it is the very first line to be executed. 'use strict' // We're on a strict mode. // Then the rest of

Overview
'use strict'
enables JavaScript strict mode. This line must be placed at the very beginning of the script, otherwise strict mode will not work. In strict mode, the interpreter will explicitly throw errors on actions that it previously skipped. Once strict mode has been enabled, it cannot be disabled for a file.
'use strict' // Place the line at the very beginning of the file.
// Another code...
Strict mode was introduced to JavaScript with the ECMAScript 5 standard in 2009.
This standard added many new features to the language, but in order to keep older browsers running, a special strict execution mode was introduced that included new language features.
A rigorous regime does the following:
- Throws errors when some unsafe constructs are used in the code.
- Turns off language features that confuse the code and therefore should not be used.
- Prevents the use of words that may be used as keywords in the future.
use strict
Restrictions
You cannot use variables without a declaration
The strict mode interpreter will throw an error if you access a variable without declaring it:
'use strict'
const name = 'Alice'
console.log(name)// Alice
age = 18
console.log(age) // Uncaught ReferenceError: age is not defined
Without strict mode, the interpreter will then create the variable age
in the global scope. If you execute the code from the example in the browser console without 'use strict'
, everything will be executed without errors, and the age
field with the value 18
will be written to the global window
object.
Clear error if the field value cannot be changed or deleted
Using the Object.defineProperty()
or Object.preventExtensions()
methods in JavaScript, you can prevent object fields from being overwritten. If strict mode is enabled, an attempt to overwrite a field will result in an error.
'use strict'
const obj = {}
Object.defineProperty(obj, 'newProp', { value: 'Alice', writable: false })
console.log(obj.newProp) // Alice
obj.newProp = 'James' // Uncaught TypeError: Cannot assign to read only property 'newProp' of object #
'use strict'
const notExtensableObj = {}
Object.preventExtensions(notExtensableObj)
notExtensableObj.newProp = 'Value' // Uncaught TypeError: Can't add property newProp, object is not extensible.
An error will be thrown in strict mode and when attempting to remove a field from an object when it cannot be done.
const obj = {}
Object.defineProperty(obj, 'newProp', {
value: 'Anna',
configurable: false
})
delete obj.newProp // Uncaught TypeError: Cannot delete property 'newProp' of #
If you run all the code samples above without strict mode, they will execute without errors, but the field values will not change.
Function parameters cannot have the same names
If you declare two parameters with the same name in a function, strict mode will throw a runtime error.
'use strict'
// Uncaught SyntaxError: Duplicate parameter name not allowed in this context:
function sum(a, b, a) {
// ...
}
Without use strict
, the interpreter will execute the code without errors, but it will be impossible to access the overridden parameter.
function sum(a, b, a) {
console.log(a)
console.log(b)
}
sum(1, 2, 3) // Prints 3 and 2, the first argument is lost forever.
Other behaviour this
With strict mode enabled, this
will no longer default to referring to a global object.
'use strict'
function logThis() {
console.log(this)
}
logThis() // undefined
Without use strict
, if you call a function in a global context (e.g. in a browser console), this
will always reference the global object.
It is forbidden to use reserved words
In strict mode it is forbidden to use in code some words that have been specially reserved to be used in the future. These are the words implements
, interface,
let
, package
, private
, protected
, public
, static
, yield
. Some of these words are already used at the moment: for example, declaring variables via let
or returning a value from a generator via yield
.
Restriction of unsafe designs
Additionally, enabling strict mode prevents you from using the with
construct in your code and clears variables created with eval()
. Both of these constructs are deprecated and potentially dangerous, so they are not used in modern JavaScript.
How to Spell
All modern JavaScript builders know how to add
'use strict'
to files themselves. That's why nobody usually declares strict mode explicitly in code at the moment. And with the advent of JavaScript modules, they all run in strict mode by default.
'use strict'
is called a directive because it is not just a string, but a special instruction to the interpreter. Therefore, it is very important to put the strict declaration at the very beginning of the script, so that it is the very first line to be executed.
'use strict' // We're on a strict mode.
// Then the rest of the code...
If you place the line below, the strict mode will not be enabled.
console.log(3 + 2) // 5
'use strict' // Just a string declaration, strict mode was not enabled.
// The code below works without strict mode...
Only comments can be placed above 'use strict'
.
Support ❤️
It took a lot of time and effort to create this material. If you found this article useful or interesting, please support my work with a small donation. It will help me to continue sharing my knowledge and ideas.
Make a contribution or Subscription to the author's content: Buy me a Coffee, Patreon, PayPal.