Bird
Raised Fist0
Javascriptprogramming~20 mins

Global scope in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
πŸŽ–οΈ
Global Scope Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this code involving global and local variables?

Consider the following JavaScript code:

var x = 10;
function test() {
  var x = 20;
  console.log(x);
}
test();
console.log(x);

What will be printed to the console?

Javascript
var x = 10;
function test() {
  var x = 20;
  console.log(x);
}
test();
console.log(x);
A20\n10
B10\n20
C20\n20
D10\n10
Attempts:
2 left
πŸ’‘ Hint

Remember that variables declared inside a function have local scope and do not affect the global variable.

❓ Predict Output
intermediate
2:00remaining
What happens when you omit var/let/const in a function?

Look at this code:

function setValue() {
  y = 5;
}
setValue();
console.log(y);

What will be the output?

Javascript
function setValue() {
  y = 5;
}
setValue();
console.log(y);
ATypeError
BReferenceError: y is not defined
Cundefined
D5
Attempts:
2 left
πŸ’‘ Hint

Variables assigned without var, let, or const inside functions become global.

🧠 Conceptual
advanced
2:00remaining
Which statement about global scope is true?

Choose the correct statement about global scope in JavaScript.

AVariables declared with <code>let</code> at the top level become properties of the global object.
BVariables declared with <code>var</code> at the top level become properties of the global object.
CUsing <code>const</code> inside a function creates a global variable.
DFunctions declared inside blocks are always global.
Attempts:
2 left
πŸ’‘ Hint

Think about how var and let differ in global scope.

❓ Predict Output
advanced
2:00remaining
What is the output when modifying a global variable inside a function?

Consider this code:

let count = 0;
function increment() {
  count++;
}
increment();
console.log(count);

What will be printed?

Javascript
let count = 0;
function increment() {
  count++;
}
increment();
console.log(count);
AReferenceError
BNaN
C1
D0
Attempts:
2 left
πŸ’‘ Hint

Variables declared with let at global scope can be accessed and changed inside functions.

❓ Predict Output
expert
3:00remaining
What is the output of this code with nested scopes and global variable?

Analyze this code snippet:

var a = 1;
function outer() {
  var a = 2;
  function inner() {
    a = 3;
  }
  inner();
  console.log(a);
}
outer();
console.log(a);

What will be printed to the console?

Javascript
var a = 1;
function outer() {
  var a = 2;
  function inner() {
    a = 3;
  }
  inner();
  console.log(a);
}
outer();
console.log(a);
A3\n1
B3\n3
C2\n3
D2\n1
Attempts:
2 left
πŸ’‘ Hint

Remember that inner changes the a in outer's scope, not the global a.

Practice

(1/5)
1. What does it mean when a variable is in the global scope in JavaScript?
easy
A. The variable can be accessed anywhere in the code.
B. The variable can only be used inside a function.
C. The variable is hidden from all parts of the code.
D. The variable is deleted after a function finishes.

Solution

  1. Step 1: Understand global scope meaning

    Global scope means a variable is declared outside any function and is accessible everywhere in the code.
  2. Step 2: Compare options with definition

    Only The variable can be accessed anywhere in the code. correctly states that the variable can be accessed anywhere.
  3. Final Answer:

    The variable can be accessed anywhere in the code. -> Option A
  4. Quick Check:

    Global scope = accessible everywhere [OK]
Hint: Global variables are outside functions, accessible everywhere [OK]
Common Mistakes:
  • Thinking global variables are only inside functions
  • Confusing global with local scope
  • Believing global variables disappear after function ends
2. Which of the following is the correct way to declare a global variable in JavaScript?
easy
A. Inside a function using let
B. Outside any function using var
C. Inside a block using const
D. Inside a function using const

Solution

  1. Step 1: Identify where to declare global variables

    Global variables must be declared outside any function or block.
  2. Step 2: Check declaration keywords

    Using var outside functions creates a global variable. Options A, B, and D declare inside functions or blocks, so they are not global.
  3. Final Answer:

    Outside any function using var -> Option B
  4. Quick Check:

    Global variable = declared outside functions [OK]
Hint: Declare global variables outside functions with var [OK]
Common Mistakes:
  • Declaring variables inside functions and expecting global scope
  • Using let or const inside blocks thinking they are global
  • Confusing block scope with global scope
3. What will be the output of this code?
var x = 10;
function show() {
  console.log(x);
}
show();
medium
A. 10
B. undefined
C. ReferenceError
D. null

Solution

  1. Step 1: Identify variable scope

    Variable x is declared globally with var and has value 10.
  2. Step 2: Check function access

    The function show logs x. Since x is global, it is accessible inside the function.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Global variable accessed inside function = 10 [OK]
Hint: Global variables are accessible inside functions [OK]
Common Mistakes:
  • Expecting undefined because variable is outside function
  • Thinking console.log will cause ReferenceError
  • Confusing local and global scope
4. Find the error in this code related to global scope:
function test() {
  var a = 5;
}
console.log(a);
medium
A. No error, prints 5
B. TypeError: a is undefined
C. ReferenceError: a is not defined
D. SyntaxError: var inside function

Solution

  1. Step 1: Check variable scope

    Variable a is declared inside function test with var, so it is local to that function.
  2. Step 2: Access variable outside function

    Trying to log a outside the function causes a ReferenceError because a is not in global scope.
  3. Final Answer:

    ReferenceError: a is not defined -> Option C
  4. Quick Check:

    Local variable accessed globally = ReferenceError [OK]
Hint: Variables inside functions are not global [OK]
Common Mistakes:
  • Assuming var inside function is global
  • Expecting output 5 instead of error
  • Confusing ReferenceError with SyntaxError
5. You want to create a global counter variable that increases every time a function runs. Which code correctly uses global scope to do this?
hard
A.
let counter = 0;
function count() {
  let counter = 0;
  counter++;
  console.log(counter);
}
B.
function count() {
  var counter = 0;
  counter++;
  console.log(counter);
}
C.
function count() {
  let counter = 0;
  counter++;
  console.log(counter);
}
D.
var counter = 0;
function count() {
  counter++;
  console.log(counter);
}

Solution

  1. Step 1: Identify global variable declaration

    var counter = 0;
    function count() {
      counter++;
      console.log(counter);
    }
    declares counter outside the function, making it global and accessible inside the function.
  2. Step 2: Check counter increment behavior

    Inside count, counter++ increases the global variable, so the count persists across calls.
  3. Final Answer:

    var counter = 0; function count() { counter++; console.log(counter); } -> Option D
  4. Quick Check:

    Global counter increments inside function =
    var counter = 0;
    function count() {
      counter++;
      console.log(counter);
    }
    [OK]
Hint: Declare counter outside function to keep it global [OK]
Common Mistakes:
  • Declaring counter inside function resets it each call
  • Using let inside function hides global variable
  • Expecting local counter to remember previous value
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard