JavaScript – Strict Mode


What is strict mode in javascript ?

“Strict Mode”, this is a new feature in ECMAScript 5.

This allows you to place a program, or a function, in a “strict” operating context.

This “use strict” prevents certain actions from being taken and throws more exceptions.

Why do we use “use strict” ?

There are some benefits in javascript implementing the mode “use strict”, listed below

1. This mode eliminates some hidden/silent errors by throwing errors.

2. This mode fixes error and helps Javascript engine for optimizations.

3. This mode helps to run the identical codes run faster.

4. This mode prohibits some syntax as per the versions of ECMAScript.

5. This mode prevents and also throws errors when “unsafe” actions were initiated.

6. This helps us to write secure Javascript code.

What are the other reserved keywords which cannot be used as variables in strict mode ?

There are some of the reserved keywords for future javascript versions, which cannot be used as variable names in strict mode.

1. implements


2. interface

3. let

4. package

5. private

6. protected

7. public

8. static

9. yield

How to use strict-mode ?

There are two ways we can use “use strict” mode.

1. By declaring globally

2. By declaring inside the function

Javascript advanced course !!!

Click to Learn More about – Javascript online learning

1. By declaring globally

Example :

<script>
		"use strict";
		a = 10;
	</script>

Output :

2. By declaring inside the function

Example :

<script>
		function myFunction(){
		"use strict";
		a = 10;
		}
		myFunction();
	</script>

Output :

However, “this” keyword inside the functions behaves differently in strict mode.

It refers to the object that is called the function.

If the object is not specified, functions in strict mode will return “undefined”.

However, the functions in normal mode will return the global object (window):

Example :

<script>
                        "use strict";
                        function myFunction() {
                                alert(this);
                        }
                        myFunction();
                </script>

Output :

There are some of the operations which are not allowed in strict mode listed below,

1. Variable is used without declaring is not allowed in strict mode.

Example :

<script>
			"use strict";
			 a = 10;
		</script>

Output :

2. Object is used without declaring is not allowed in strict mode.

Example :

<script>
			"use strict";
			a = {a:10,b:20,c:30};
		</script>

Output :


3. Deleting a variable / object is not allowed in strict mode.

Example :

<script>
			"use strict";
			 var a = 10;
			 delete a;
		</script>

Output :


4. Deleting a function is not allowed in strict mode.

Example :

<script>
			"use strict";
			function myFunction(a,b){
				delete a;
				delete b;
			}
			myFunction();
		</script>

Output :

5. Parameter name duplication is not allowed in strict mode.

Example :

<script>
			"use strict";
			function myFunction(a,a){

			}
			myFunction();
		</script>

Output :

6. Octal numeric literals are not allowed in strict mode.

Example :

<script>
			"use strict";
			 a = 001101;
		</script>

Output :

7. Octal escape characters are not allowed in strict mode.

Example :

<script>
			"use strict";
			 var a = "\0101010";
		</script>

Output :

8. Writing to a read-only property is not allowed in strict mode.

Example :

<script>
			"use strict";
			var newObj = {};
			Object.defineProperty(newObj, "a", {value:2, writable:false});
			newObj.a = 3;
		</script>

Output :

9. Writing to a get-only property is not allowed in strict mode.

Example :

<script>
			"use strict";
			var newObj = {get a() {return 0} };
			newObj.a = 8; 
		</script>

Output :

10. Deleting an un-deletable property is not allowed in strict mode.

Example :

<script>
			"use strict";
			delete Object.prototype; 
		</script>

Output :


11. The word eval cannot be used as a variable in strict mode.

Example :

<script>
			"use strict";
			var eval = 5.23;  
		</script>

Output :


12. The word arguments cannot be used as a variable in strict mode.

Example :

<script>
			"use strict";
			var arguments = 5.23;
		</script>

Output :

13. The with statement is not allowed in strict mode.

Example :

<script>
			"use strict";
			with (Math){a = cos(5)};
		</script>

Output :

14. eval() is not allowed to create variables in the scope from which it was called for security reasons.

Example :

<script>
			"use strict";
		         eval ("var a = 10");
			 print.writeln(a);
		</script>

Output :