3

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?

6
  • 1
    declare it using windows keyword like windows.df = 324 Commented Sep 28, 2020 at 12:02
  • Okay, that helps but is there a way to turn it off? Commented Sep 28, 2020 at 12:05
  • stackoverflow.com/questions/6020178/…. Commented Sep 28, 2020 at 12:06
  • you should code with strict mode, its better Commented Sep 28, 2020 at 12:06
  • I guess so but sometimes it's just easier to get things done quickly. Commented Sep 28, 2020 at 12:07

1 Answer 1

5

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);

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.