Explanation of Strict Mode in JavaScript
Hello everyone, in this article we are going to talk about strict mode in Javascript we will see the benefits and importance of strict mode in Javascript of strict mode.Lets get started
Before we start we should know that Javascript is everywhere on the internet and it is providing us powerful structures to handle the operations easily. However javascript has no type safety definitions and users can input various types of data. So we need to make it dynamic and allow to handle the expected data types. This might make the debugging a bit difficult. In here strict mode support us and with strict mode mode we can debug the issues easily.
What is this "use strict"?
"Use strict" is a command which is introduced with EcmaScript5 to enable the strict mode and error handling in JavaScript. We use the "use strict" at the beginnin gof the script or function, and after that code will be executed within the strict mode. With it we can see the errors in the log to understand the issues better
Now lets take a look at the common errors that get caught by strict mode in Java Script
"use strict";
mistakeVar = 10;
delete Object.prototype;
var let = 10; //Strict mode will throw error due to variable name
Now lets take a look at the usages of Strict Mode in Javascript
To enable strict mode in java script we need to enable it with use strict first in the related scope.
"use strict";
When we enable it at the top of the entire script it will be enabled for all the scope of the script.
"use strict"
var a = "Thecodeprogram";
a = 10; // Error will be throwed in here
function test() {
const b = "burak";
b = true; // Error will be thrown in here too
}
test();
We can enable strict mode for a function. To enable it in a function scope we need to call use strict in the relevant function.
var a = "Thecodeprogram";
a = 10;
function myFnc() {
"use strict" //Enable strict mode in current function scope
const b = "burak";
b = true; // Will caughted by strict mode
}
That is all for this article.
Burak Hamdi Tufan
Comments