What JS scope is used by – Peter Krebs Feb 15 '23 at 10:55

  • So what is global scope ? How does the console read from it ? – Dimitri Kopriwa Feb 15 '23 at 11:05
  • 1
    Check [this answer](https://stackoverflow.com/a/4862268/1169519) to see the difference between variable scope and object properties. – Teemu Feb 15 '23 at 11:06
  • 1 Answers1

    3

    The script tag is just that...An HTML tag. As such, it doesn't have a scope because it's not JavaScript, it's HTML. What the script element does for your is allows the HTML parser to "stand down" and have the JavaScript interpreter take over. The JS within the script tag will execute according to standard JS rules. That means that references will be evaluated via the normal scope chain process.

    In essence, everything in JS is global unless you code it to be otherwise.

    Keep in mind that window is not part of JavaScript. It's an API provided by browsers, so when we talk about "global", we are not always talking about window. In node.js, for example, there is no window or document, but there still is a global scope. So, in JS, you can have globals, but that doesn't mean that they are automatically attached as properties of the window object.

    • Globals in JS are accessible by simply stating their reference name.
    • Any HTML element with an ID attribute will become a property of the window object.
    • Any JS property explicitly created as part of the window object will be available via the window object.
    • Any HTML element that is part of the page that is loaded will be accessible via a DOM API (i.e., document.querySelector, document.getElementById).

    This is another answer from a while back that talks about the "scope chain" and how it works.

    As @Teemu points out in the comments below, when you have a script type="module", things work a bit differently. See this link for more details.

    Scott Marcus
    • 64,069
    • 6
    • 49
    • 71
    • 1
      Would it be worth of mention, that scripts type of module are behaving differently? I know OP didn't specifically ask about modules, but for future readers perhaps. – Teemu Feb 15 '23 at 11:25
    • 2
      @Teemu That's a good point. I think that rather than explain that here though, I will edit to make sure that it's understood that they are a bit different. – Scott Marcus Feb 15 '23 at 11:29
    • In this case, how can I attach a variable to global scope from within a class ? And how can I access a global variable using a string ? – Dimitri Kopriwa Feb 15 '23 at 12:15
    • Use StackOverflows search and just type in that exact question. For example here: [Define a global variable in a JavaScript function](https://stackoverflow.com/questions/5786851/define-a-global-variable-in-a-javascript-function). The function can also be in a class. The variable must have been declared beforehand. – Peter Krebs Feb 15 '23 at 12:25
    • 1
      @DimitriKopriwa If you read the link in my answer, you'll see that you don't define a global from within a class. JavaScript is lexically scoped. This means that *where* you declare your variable determines what its scope is. – Scott Marcus Feb 15 '23 at 12:56