Introduction
In this blog, I am going to discuss about strict mode in JavaScript. We can use strict mode in two way in a JavaScript code,
- On the page level.
- On the function level.
On the page level,
- <script>
- "use strict"
- GetPiValue();
-
- function GetPiValue() {
- y = 3.14;
- console.log(y);
- }
- </script>
In the above code, we used "use strict" at top of the page. When we run it, we are getting the following error.
On the function level,
- <script>
- GetPiValue();
-
- function GetPiValue() {
- "use strict"
- y = 3.14;
- console.log(y);
- }
- </script>
Again, in the above code, we used "use strict" inside the method. When we run it, we are getting the same error.
Why we are getting the above error for using "use strict".?
We use this mode for better coding and error checking in JavaScript code. If we use strict mode, then we are not allowed to do many things. Below are some operations you cannot perform in strict mode.
- Assigning value to non-declared variable
- Assigning value to read only variable
- Defining duplicate property in a function
- Cannot define a function inside else-if or for statement
Many other operations will give error for using "use strict". I hope you have understood the purpose of this article . Thanks for reading.