Basic JavaScript - Global Scope and Functions

Tell us what’s happening:
Hi, this line “Variables which are declared without the let or const keywords are automatically created in the global scope”
states that a variable declared with var would have global scope, however this seems to be incorrect? I think var would be function scope, while let would be block scope? this has been confusing me

  **Your code so far**
// Declare the myGlobal variable below this line

function fun1() {
// Assign 5 to oopsGlobal Here


// Only change code above this line

function fun2() {
var output = "";
if (typeof myGlobal != "undefined") {
  output += "myGlobal: " + myGlobal;
if (typeof oopsGlobal != "undefined") {
  output += " oopsGlobal: " + oopsGlobal;
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Basic JavaScript - Global Scope and Functions

Link to the challenge:

Inside function fun1 , assign 5 to oopsGlobal without using the var , let or const keywords.

var can be function scoped :slightly_smiling_face:
It’s global if you don’t use any of the keywords. The explanation in that example sure is a bit misleading…
Here is an article that explains more about the scopes that should be more helpful:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.