What is Modern Mode/ Strict Mode in JS

·

2 min read

So what is this “use Strict” mode as the name suggests it enables the strict mode when we write JS code.

Before 2009, there were some bugs in JavaScript to solve these bugs JavaScript team come up with the "use Strict" mode in 2009 with an ES5 update.

sometimes use strict mode also refers as Modern Mode.

The purpose of the strict mode is to indicate the code that should be executed in ‘strict mode”.

What does it enables and what Features?

  1. You can not use undeclared variables

     "use strict";
     x = 10; //ReferenceError: x is not defined
    
     //Solution
    
     "use strict";
     let x = 10;
    
  2. The Same Parameters are not accepted

     "use strict";
     function funName(a,a){
     return a + a;
     }// SyntaxErr: Argument name clash
    
  3. Using an object without declaring it is not allowed

     "use strict";
     x ={
     a:20,
     b:10
     }//RefErr x is not defined
    
     //Solution
    
     "use strict";
     let x = {
     a:20,
     b:10
     }
    
  4. Deleting a variable or Object is not allowed

     'use strict'
     var x= 10;
     delete x; //SyntaxError Deleting local variable in strict mode.
    

How to apply "use Strict" mode

we can apply “use strict” mode anywhere in the global scope or in a particular function or a block of code

When we apply to the top of the code that's become the global strict mode in that all code/ files will come under the strict mode

when we apply a particular function/ block of code then the function or block of code becomes Strict

'use strict' //Global strict mode 
function funName(){ 
'use strict' //Local strict mode.Code inside this function is only in strict mode 
return 0
}

Thanks for reading Keep Learning 👍