We use var to create a named container that holds a value we can use later in our program.
Variable declaration using var in Javascript
Introduction
Syntax
Javascript
var variableName = value;The var keyword declares a variable.
You can assign a value right away or leave it undefined.
Examples
age and stores the number 25 in it.Javascript
var age = 25;
name and stores the text "Alice".Javascript
var name = "Alice";
isStudent without giving it a value yet.Javascript
var isStudent;var variable anytime.Javascript
var score = 10; score = 15;
Sample Program
This program creates two variables and prints a greeting message using them.
Javascript
var greeting = "Hello"; var name = "Bob"; console.log(greeting + ", " + name + "!");
Important Notes
var variables are function-scoped, which means they are visible throughout the function they are declared in.
Unlike let and const, var allows redeclaration of the same variable name in the same scope.
Modern JavaScript prefers let and const for clearer scoping rules, but var is still important to understand.
Summary
var creates a variable to store data.
Variables declared with var can be changed and redeclared.
var variables are function-scoped, not block-scoped.
Practice
1. What does the
var keyword do in JavaScript?easy
Solution
Step 1: Understand
varbehaviorvardeclares variables that can be changed and redeclared in the same scope.Step 2: Compare with other declarations
Unlikeconstorlet,varis function-scoped and allows redeclaration.Final Answer:
Declares a variable that can be changed and redeclared -> Option CQuick Check:
varallows 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
Solution
Step 1: Recall correct
The correct syntax isvarsyntaxvar variableName = value;with no spaces or colons.Step 2: Check each option
Only var myVar = 10; matches the correct syntax exactly.Final Answer:
var myVar = 10; -> Option AQuick Check:
Correctvarsyntax = 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
Solution
Step 1: Understand redeclaration with var
Usingvar, redeclaring a variable overwrites its previous value.Step 2: Trace the code execution
Firstxis 5, then redeclared as 10, soconsole.log(x)prints 10.Final Answer:
10 -> Option DQuick 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
Solution
Step 1: Understand var scope
varis function-scoped, not block-scoped, soais accessible outside the if block.Step 2: Check console.log output
Sinceais declared withvar,console.log(a)prints 1 without error.Final Answer:
No error, prints 1 -> Option BQuick Check:
varis 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:
What will be the output and why?
function test() {
if (true) {
var count = 5;
}
var count = 10;
return count;
}
console.log(test());What will be the output and why?
hard
Solution
Step 1: Analyze var declarations inside function
Bothvar countdeclarations are in the same function scope, so the second redeclaration overwrites the first.Step 2: Determine returned value
The function returns the last assigned value, which is 10.Final Answer:
10 because second var redeclares and overwrites -> Option AQuick 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
10 questions ~5 mineasy hard
