Saturday, August 21, 2021

Use of 'strict mode' in Javascript for Writing Secure Code

Strict Mode in JS : Strict mode makes it easier to write "secure" JavaScript.

If you write programme in Javascript and sometimes you mispelled any variable, in this condition code not work and not throw error back. But if you use strict mode You will get error. and you can solve your error problem easily.

If you use Javascript reserved word in your variable it will throw an error because you are using strict mode. but without strict mode you can not resolve the problem. so use always strict mode in your Javascript Programme.

// Without using strict mode run this code..

let hasDriversLicence = false;
let hasPassTest = true;
if (hasPassTest) {
hasDriverLicence = true; // (We Mistype hasDriversLicence Here)
}
if (hasDriversLicence) {
console.log(`She can Drive a Car`);
}
// This code will not show any error...

// With strict mode run this code..

'use strict';
let hasDriversLicence = false;
let hasPassTest = true;
if (hasPassTest) {
hasDriverLicence = true; // (We Mistype hasDriversLicence Here)
}
if (hasDriversLicence) {
console.log(`She can Drive a Car`);
}
// This code will show an error...

0 comments

Post a Comment