Bird
Raised Fist0
Javascriptprogramming~20 mins

Variable declaration using var in Javascript - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Var Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of var variable hoisting
What is the output of this JavaScript code?
console.log(x);
var x = 5;
console.log(x);
Javascript
console.log(x);
var x = 5;
console.log(x);
Aundefined\n5
Bundefined\nundefined
C5\n5
DReferenceError: x is not defined
Attempts:
2 left
💡 Hint
Think about how var variables are hoisted but not initialized.
❓ Predict Output
intermediate
2:00remaining
var variable scope inside function
What will be the output of this code?
function test() {
var a = 10;
if (true) {
var a = 20;
console.log(a);
}
console.log(a);
}
test();
Javascript
function test() {
  var a = 10;
  if (true) {
    var a = 20;
    console.log(a);
  }
  console.log(a);
}
test();
A10\n20
B20\n20
C20\n10
DReferenceError
Attempts:
2 left
💡 Hint
Remember var is function scoped, not block scoped.
❓ Predict Output
advanced
2:00remaining
var redeclaration behavior
What is the output of this code?
var x = 1;
var x = 2;
console.log(x);
Javascript
var x = 1;
var x = 2;
console.log(x);
ASyntaxError
B1
C2
DReferenceError
Attempts:
2 left
💡 Hint
var allows redeclaration without error.
❓ Predict Output
advanced
2:00remaining
var variable in loops and closures
What will this code print?
var funcs = [];
for (var i = 0; i < 3; i++) {
funcs.push(function() { return i; });
}
console.log(funcs[0]());
console.log(funcs[1]());
console.log(funcs[2]());
Javascript
var funcs = [];
for (var i = 0; i < 3; i++) {
  funcs.push(function() { return i; });
}
console.log(funcs[0]());
console.log(funcs[1]());
console.log(funcs[2]());
AReferenceError
B0\n1\n2
Cundefined\nundefined\nundefined
D3\n3\n3
Attempts:
2 left
💡 Hint
var is function scoped, so all functions share the same i.
🧠 Conceptual
expert
2:00remaining
var variable redeclaration and hoisting combined
Consider this code snippet:
console.log(a);
var a = 10;
var a = 20;
console.log(a);

What will be the output when this code runs?
Javascript
console.log(a);
var a = 10;
var a = 20;
console.log(a);
Aundefined\n20
B10\n20
CReferenceError\n20
Dundefined\n10
Attempts:
2 left
💡 Hint
var declarations are hoisted and redeclarations overwrite previous values.

Practice

(1/5)
1. What does the var keyword do in JavaScript?
easy
A. Declares a block-scoped variable
B. Declares a constant that cannot be changed
C. Declares a variable that can be changed and redeclared
D. Declares a variable only inside loops

Solution

  1. Step 1: Understand var behavior

    var declares variables that can be changed and redeclared in the same scope.
  2. Step 2: Compare with other declarations

    Unlike const or let, var is function-scoped and allows redeclaration.
  3. Final Answer:

    Declares a variable that can be changed and redeclared -> Option C
  4. Quick Check:

    var allows redeclaration = A [OK]
Hint: Remember: var variables can be changed and redeclared easily [OK]
Common Mistakes:
  • Confusing var with const or let
  • Thinking var is block-scoped
  • Assuming var variables cannot be redeclared
2. Which of the following is the correct syntax to declare a variable using var?
easy
A. var myVar = 10;
B. variable myVar = 10;
C. var: myVar = 10;
D. v ar myVar = 10;

Solution

  1. Step 1: Recall correct var syntax

    The correct syntax is var variableName = value; with no spaces or colons.
  2. Step 2: Check each option

    Only var myVar = 10; matches the correct syntax exactly.
  3. Final Answer:

    var myVar = 10; -> Option A
  4. Quick Check:

    Correct var syntax = A [OK]
Hint: Use 'var name = value;' format without extra symbols [OK]
Common Mistakes:
  • Using 'variable' instead of 'var'
  • Adding colons after var
  • Adding spaces inside 'var'
3. What will be the output of this code?
var x = 5;
var x = 10;
console.log(x);
medium
A. 5
B. Error
C. undefined
D. 10

Solution

  1. Step 1: Understand redeclaration with var

    Using var, redeclaring a variable overwrites its previous value.
  2. Step 2: Trace the code execution

    First x is 5, then redeclared as 10, so console.log(x) prints 10.
  3. Final Answer:

    10 -> Option D
  4. Quick Check:

    Redeclared var overwrites value = 10 [OK]
Hint: Redeclaring var overwrites previous value [OK]
Common Mistakes:
  • Thinking redeclaration causes error
  • Expecting first value to print
  • Confusing var with let or const behavior
4. Find the error in this code snippet:
if (true) {
  var a = 1;
}
console.log(a);
medium
A. ReferenceError: a is not defined
B. No error, prints 1
C. TypeError: a is not a function
D. SyntaxError due to missing braces

Solution

  1. Step 1: Understand var scope

    var is function-scoped, not block-scoped, so a is accessible outside the if block.
  2. Step 2: Check console.log output

    Since a is declared with var, console.log(a) prints 1 without error.
  3. Final Answer:

    No error, prints 1 -> Option B
  4. Quick Check:

    var is function-scoped, so prints 1 [OK]
Hint: var ignores block scope, accessible outside blocks [OK]
Common Mistakes:
  • Assuming var is block-scoped like let
  • Expecting ReferenceError outside block
  • Confusing var with const or let scope
5. Consider this code:
function test() {
  if (true) {
    var count = 5;
  }
  var count = 10;
  return count;
}
console.log(test());

What will be the output and why?
hard
A. 10 because second var redeclares and overwrites
B. Error due to redeclaration of count
C. 5 because first var inside if is used
D. undefined because var is block-scoped

Solution

  1. Step 1: Analyze var declarations inside function

    Both var count declarations are in the same function scope, so the second redeclaration overwrites the first.
  2. Step 2: Determine returned value

    The function returns the last assigned value, which is 10.
  3. Final Answer:

    10 because second var redeclares and overwrites -> Option A
  4. Quick Check:

    Function-scoped var redeclaration overwrites = 10 [OK]
Hint: Inside functions, var redeclaration overwrites previous value [OK]
Common Mistakes:
  • Thinking redeclaration causes error
  • Assuming block scope limits var
  • Expecting first value to be returned
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