I have a script tag <script type="module" src="d.js"></script> and In that script tag I define a global variable like this df = 324. When I do this I get a reference error Uncaught ReferenceError: df is not defined. Why is this?
- 1declare it using windows keyword like windows.df = 324Rabby– Rabby2020-09-28 12:02:49 +00:00Commented Sep 28, 2020 at 12:02
- Okay, that helps but is there a way to turn it off?ethtrhaef– ethtrhaef2020-09-28 12:05:10 +00:00Commented Sep 28, 2020 at 12:05
- stackoverflow.com/questions/6020178/….Ilijanovic– Ilijanovic2020-09-28 12:06:10 +00:00Commented Sep 28, 2020 at 12:06
- you should code with strict mode, its betterIlijanovic– Ilijanovic2020-09-28 12:06:43 +00:00Commented Sep 28, 2020 at 12:06
- I guess so but sometimes it's just easier to get things done quickly.ethtrhaef– ethtrhaef2020-09-28 12:07:24 +00:00Commented Sep 28, 2020 at 12:07
| Show 1 more comment
1 Answer
Because modules are by default in strict mode
https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Strict_mode
If you try:
"use strict"; df = 324; It wont work either
"use strict"; df = 324; Without
df = 324; console.log(df);