

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is Strict mode in JavaScript?
Strict mode
Strict mode was introduced by ECMAScript 5 to javascript. Using strict mode javascript silent errors can be easily detected as they would throw an error. This makes javascript debugging much easy and helps developers to avoid unnecessary mistakes.
Since strict mode throws an exception when an undeclared variable is encountered, memory leaks will be greatly reduced. The strict mode can be enabled by using "use strict" in front of the code where strict mode is required.
In the following example two variables were used, one is outside the function and other one is inside the function. The variable that is used outside the function is not declared, whereas the variable that is declared inside a function is declared using var keyword. Using strict mode inside the function don't throw any error because variable is declared at the same time value in the variable outside the function will be displayed because there is no strict mode used.
Example-1
<html> <body> <script> myString1 = "non-strict mode will allow undeclared variables" document.write(myString1); document.write("</br>"); function myFun(){ "use strict" var myString2 = "Strict mode will allow declared variables" document.write(myString2); } myFun(); </script> </body> </html>
Outputnon-strict mode will allow undeclared variables
Strict mode will allow declared variables
In the following example, variable is not declared inside the function and strict mode is applied. So value inside that variable won't be executed and throws error. We can found the error in browser console.
Example-2
<html> <body> <script> myString1 = "non-strict mode will allow undeclared variables" document.write(myString1); document.write("</br>"); function myFun(){ "use strict" myString2 = "Strict mode will allow declared variables" document.write(myString2); } myFun(); </script> </body> </html>
Outputnon-strict mode will allow undeclared variables
- Related Questions & Answers
- What is Strict Mode in JavaScript and How to Enable It?
- Strict Mode in ReactJS
- How to enable a strict mode in javascript?
- What are the characteristics of JavaScript 'Strict Mode'?
- How to secure my JavaScript using "Strict mode"?
- What is the difference between "strict" and "non-strict" modes of JavaScript?
- What does “use strict” do in JavaScript, and what is the reasoning behind it?
- Explain Strict Comparison in JavaScript switch statement?
- Strict equality vs Loose equality in JavaScript.
- Why strict aliasing is required in C?
- Is it safe to assume strict comparison in a JavaScript switch statement?
- What is simplex mode of transmission in computer networks?
- What is the use of IGNORE_SPACE SQL mode?
- What is the use of ALLOW_INVALID_DATES SQL mode?
- What is the architecture of Asynchronous Transfer Mode?