Table of contents
No headings in the article.
What is "Strict Mode" in javascript?
"Strict Mode" is used in Javascript that allows preventing actions in javascript code and throws more exceptions. It was first introduced in ECMAScript 5 and helps to avoid bad syntax, and makes javascript code secure.
For Instance:
//without strict mode
str =" How are you";
document.write(str);
This code will not show any error and get executed successfully but assigning value to the variable before the declaration, is counted as bad practice for coders. it also affects the final output. , When there is a large project it becomes tough to debug the code.
//with strict mode
'use strict'
str = "How are you"; // throw a reference error
document.write(str);
as you can clearly see, the use of 'use strict' is giving a reference error for the same code as above.
so how can you correct it?
*declare the variable first before assigning it a value.
'use strict'
Let str = "How are you";
document.write(str);
What are the other things "strict mode" doesn't allow?
- Use of undefined variables.
- Use of reserved keywords as variable or function names (such as return, if, else, etc.)
- Duplicate properties of an object
- Duplicate parameters of a function.
- Assign values to read-only properties
- Modifying arguments object
- Octal numeric literals.
- with statement.
- eval function to create a variable
- Using an object without declaring it.
Advantages of using "strict mode":
- Eliminate JavaScript silent errors by throwing errors.
- Fixes mistake that makes it difficult for the JavaScript engine to perform optimization.
- Make code run faster sometimes than identical code that's not in strict mode.
Follow for more!!