
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
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 Articles
- What is Strict Mode in JavaScript and How to Enable It?
- Strict Mode in ReactJS
- 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?
- How do I disable Strict Mode in MySQL?
- 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 do we use "use strict" in JavaScript?
- Is it safe to assume strict comparison in a JavaScript switch statement?
- What is Mode?
- Why strict aliasing is required in C?
- What is the mode of nutrition in amoeba?
- What is the mode of nutrition in fungi?
