Course
Strict Mode
JavaScript Tutorial
This JavaScript tutorial is crafted for beginners to introduce them to the basics and advanced concepts of JavaScript. By the end of this guide, you'll reach a proficiency level that sets the stage for further growth. Aimed at empowering you to progress towards becoming a world-class software developer, this tutorial paves the way for a successful career in web development and beyond.
Strict Mode
Strict Mode in JavaScript
In JavaScript, the strict mode was introduced in ES5 (ECMAScript 2009) to enhance code security.
The use strict expression enables strict mode in JavaScript, eliminating silent errors like using undeclared variables or modifying read-only object properties.
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 Detection: Strict mode helps catch common coding errors and throws more exceptions.
- Prevents Silent Errors: In non-strict mode, some errors may fail silently but strict mode makes them visible.
- Secure Code: It prevents certain actions that could be considered unsafe or deprecated.
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 the error in the console and doesn't print the output.
In short, the strict mode doesn't allow you to use the variable without its declaration.
<html><head> <title> Using the strict mode gloablly </title></head><body> <script> "use strict"; let y = 50; // This is valid document.write("The value of the X is: " + y); x = 100; // This is not valid document.write("The value of the X is: " + x); </script></body></html>
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.
<html><head> <title> Using the strict mode gloablly </title></head><body> <script> x = 100; // This is valid document.write("The value of the X is - " + x); function test() { "use strict"; y = 50; // This is not valid document.write("The value of the y is: " + x); } test(); </script></body></html>
Mistakes that you should't make in the strict mode
1. You can't initialize the variable with a value without declaring it.
<script> 'use strict'; num = 70.90; // This is invalid</script>
2. Similarly, you can't use the object without declaring it.
<script> 'use strict'; numObj = {a: 89, b: 10.23}; // This is invalid</script>
3. You can't delete objects using the delete keyword.
<script> 'use strict'; let women = { name: "Aasha", age: 29 }; delete women; // This is invalid</script>
4. 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>
5. Deleting the function using the delete operator is not allowed.
<script> 'use strict'; function func() { } delete func; // This is invalid</script>
6. 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>
7. You can't assign octal numbers to variables.
<script> 'use strict'; let octal = 010; // Throws an error</script>
8. You can't use escape characters.
<script> 'use strict'; let octal = \010; // Throws an error</script>
9. You can't use reserved keywords like eval, arguments, public, etc., as an identifier.
<script> 'use strict'; let public = 100; // Throws an error</script>
10. 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>
11. 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>
12. 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>
13. 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>
14. You can't use the
eval()
function to declare the variables for the security reason.<script> 'use strict'; eval("a = 8")</script>
15. 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