Have you ever noticed use of strict keyboard as shown in following image anywhere?
When I start creating application for Windows Store (Metro App), I noticed this on the top of the default.js file. Well it is nothing but a way to tell JavaScript for better coding and error checking in JavaScript code. If you put strict mode then you are not allowed to do many things in JavaScript code like assigning value to a read only variable.
You can put strict mode restriction
- On the function level
- On the page level
You can put strict restriction on function level as following
Now let us try to put some code in above function. In below code we are assigning value to a variable which is not declared. We have also put function is the strict mode.
When try to call this JavaScript function, you will get error as following. You see in the error message that “Variable undefined in strict mode”. If you go ahead and remove strict mode from the function then you should able to call the function.
If you have put strict mode then you will get error if you try to perform following operations.
- Assigning value to non-declared variable
- Assigning value to read only variable
- Defining duplicate property
- Defining duplicate parameters in a function
- Using future reserved keywords like interface, let, package etc.
- Cannot defined a function inside if-else or for statement
There are many other operations will give you error in strict mode. I hope now purpose of strict mode is clear to you. Thanks for reading!
Follow @debug_mode
Leave a Reply