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?
Asked
Active
Viewed 610 times
2

ethtrhaef
- 57
- 5
-
1declare it using windows keyword like windows.df = 324 – Rabby Sep 28 '20 at 12:02
-
Okay, that helps but is there a way to turn it off? – ethtrhaef Sep 28 '20 at 12:05
-
https://stackoverflow.com/questions/6020178/can-i-disable-ecmascript-strict-mode-for-specific-functions#:~:text=No%2C%20you%20can't%20disable%20strict%20mode%20per%20function.&text=Notice%20how%20we%20can%20define,that%20strict%20immediately%20invoked%20function. – bill.gates Sep 28 '20 at 12:06
-
you should code with strict mode, its better – bill.gates Sep 28 '20 at 12:06
-
I guess so but sometimes it's just easier to get things done quickly. – ethtrhaef Sep 28 '20 at 12:07
-
I don't get it? why turn it off – Rabby Sep 28 '20 at 12:07
1 Answers
4
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);

bill.gates
- 14,145
- 3
- 19
- 47