JavaScript - Strict Mode
JavaScript - Strict Mode
Strict Mode in JavaScript
In JavaScript, thestrict modeis introduced in the ES5 (ECMAScript 2009). The purpose behind introducing the βstrict modeβ is to make the JavaScript code more secure.
The βuse strictβ literal expression is used to add the strict mode in the JavaScript code. It removes the silent errors from the code, such as you canβt use the variable without declaration, you canβt modify the readable property of the object, etc.
Enabling Strict Mode
To enble strcit mode, you should write the following literal expression to the top of your code β
'use strict';The βuse strictβ directive is used enable JavaScriptβs strict mode.
Why Use the Strict Mode?
Here, we have listed some reasons for using the strict JavaScript mode β
- Error Preventionβ The strict mode prevents the common errors which developers make while writing the JavaScript code, such as initializing the variable without declaration or using the reserved keywords as an identifier.
- Safer Codeβ The strict mode prevents the creation of global variables accidentally. Also, it doesnβt allow to use of statements like βwithβ, which can lead to vulnerability in the code.
- Future Compatibilityβ You can align your code with the future versions of JavaScript by using the script mode. For example, the current version of JavaScript doesnβt contain keywords like βpublicβ but is reserved for future versions. So, the strict mode wonβt allow you to use it as an identifier from now.
Strict Mode in the Global Scope
When you add the βuse strictβ at the top of the JavaScript code; it uses the strict mode for the whole code.
Example
In the example below, we have defined the βyβ variable and initialized it with the 50. The code prints the value of βyβ in the output.
Also, we initialized the variable βxβ without declaring it. So, it gives theerrorin the console and doesnβt print the output.
In short, the strict mode doesnβt allow you to use the variable without its declaration.
Strict Mode in the Local Scope
You can also use the βstrict modeβ inside the particular function. So, it will be applied only in the function scope. Letβs understand it with the help of an example.
Example
In the example below, we used the βuse strictβ literal only inside the test() function. So, it removes the unusual errors from the function only.
The code below allows you to initialize the variable without declaring it outside the function but not inside it.
Mistakes that you shouldβt make in the strict mode
- You canβt initialize the variable with a value without declaring it.
<script>
'use strict';
num = 70.90; // This is invalid
</script>- Similarly, you canβt use the object without declaring it.
<script>
'use strict';
numObj = {a: 89, b: 10.23}; // This is invalid
</script>- You canβt delete objects using the delete keyword.
<script>
'use strict';
let women = { name: "Aasha", age: 29 };
delete women; // This is invalid
</script>- You canβt delete the object prototype in the strict mode.
<script>
'use strict';
let women = { name: "Aasha", age: 29 };
delete women.prototype; // This is invalid
</script>- Deleting the function using the delete operator is not allowed.
<script>
'use strict';
function func() { }
delete func; // This is invalid
</script>- You canβt have a function with duplicate parameter values.
<script>
'use strict';
function func(param1, param1, param2) {
// Function with 2 param1 is not allowed!
}
</script>- You canβt assign octal numbers to variables.
<script>
'use strict';
let octal = 010; // Throws an error
</script>- You canβt use escape characters.
<script>
'use strict';
let octal = \010; // Throws an error
</script>- You canβt use reserved keywords like eval, arguments, public, etc., as an identifier.
<script>
'use strict';
let public = 100; // Throws an error
</script>- You canβt write to the readable property of the object.
<script>
'use strict';
let person = {};
Object.defineProperty(person, 'name', { value: "abc", writable: false });
obj1.name = "JavaScript"; // throws an error
</script>- You canβt assign values to the getters function.
<script>
'use strict';
let person = { get name() { return "JavaScript"; } };
obj1.name = "JavaScript"; // throws an error
</script>- In the strict mode, when you use the βthisβ keyword inside the function, it refers to the reference object through which the function is invoked. If the reference object is not specified, it refers to the undefined value.
<script>
'use strict';
function test() {
console.log(this); // Undefined
}
test();
</script>- You canβt use the βwithβ statement in the strict mode.
<script>
'use strict';
with (Math) {x = sin(2)}; // This will throw an error
</script>- You canβt use the eval() function to declare the variables for the security reason.
<script>
'use strict';
eval("a = 8")
</script>- You canβt use the keywords as an identifier that are reserved for the future. Below keywords are reserved for the future β
- implements
- interface
- package
- private
- protected