Strict Mode in Javascript. | Sololearn: Learn to code for FREE!


Strict Mode in Javascript.

when i use the strict mode the output is false but without strict mode the output is true why? "use strict"; function myFun(){ console.log(this==window);//it refers to the window object } myFun();

7/15/2020 1:43:59 AM

Samir Singh

1 Answer

New Answer


When strict mode is enabled "this" points to an undefined value if used within a function. undefined == window //false Such behavior prevents the silent creation of global variables this way: function bad(){ this.sth = 42; //now sth is global ..... //more code } var object = bad(); //You shouldn't call a constructor without the "new" keyword. Why? See line below↓ console.log(sth); //42 oops When strict mode is enabled you will be forced to use the new keyword. That's a good practice, strict mode enabled or not. Hope this helps.