JavaScript Strict Mode - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
JavaScript Strict Mode

JavaScript Strict Mode

JavaScript Strict Mode

Being a scripting language, sometimes the JavaScript code displays the correct result even it has some errors. To overcome this problem we can use the JavaScript strict mode.
The JavaScript provides "use strict"; expression to enable the strict mode. If there is any silent error or mistake in the code, it throws an error.

Note - The "use strict"; expression can only be placed as the first statement in a script or in a function.

JavaScript use strict Example

Example 1

Let's see the example without using strict mode.
  1. <script>  
  2. x=10;  
  3. console.log(x);  
  4. </script>  
Output:
Here, we didn't provide the type of variable. Still we are getting an output.

Javascript Strict Mode

Let's see the same example by enabling the strict mode.
  1. <script>  
  2. "use strict";  
  3. x=10;  
  4. console.log(x);  
  5. </script>  
Output:
Now, it will throw an error as the type of x is not defined.

Javascript Strict Mode

Example 2

Let's see one more example to print sum of two numbers.
  1. <script>  
  2. console.log(sum(10,20));  
  3. function sum(a,a)  
  4. {  
  5. "use strict";  
  6. return a+a;  
  7. }  
  8. </script>  
Output:
Here, an error occurs as we use duplicate elements.

Javascript Strict Mode

About Mariano

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.