What if you could stop rewriting the same number everywhere and just use a simple name instead?
Why Variable declaration using var in Javascript? - Purpose & Use Cases
Imagine you want to store a number to use later in your program. Without declaring a variable, you might try to write the number everywhere you need it, or just use it directly without a name.
This approach is slow and confusing because you have to change every place manually if the number changes. It also causes mistakes if you forget to update some places. Without a clear name, your code becomes hard to read and understand.
Using var lets you give a name to a value. You can store the value once and use the name everywhere. If the value changes, you only update it in one place. This makes your code cleaner, easier to read, and less error-prone.
console.log(10 + 20); console.log(10 + 30);
var x = 10; console.log(x + 20); console.log(x + 30);
It enables you to write clear, reusable, and easy-to-update code by naming your data.
Think of var like labeling a jar of sugar in your kitchen. Instead of guessing or remembering how much sugar to add each time, you just look at the label and use the right amount.
Variables let you name and store values.
var is a way to declare variables in JavaScript.
Using variables makes your code easier to manage and less error-prone.
Practice
var keyword do in JavaScript?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]
- Confusing var with const or let
- Thinking var is block-scoped
- Assuming var variables cannot be redeclared
var?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]
- Using 'variable' instead of 'var'
- Adding colons after var
- Adding spaces inside 'var'
var x = 5; var x = 10; console.log(x);
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]
- Thinking redeclaration causes error
- Expecting first value to print
- Confusing var with let or const behavior
if (true) {
var a = 1;
}
console.log(a);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]
- Assuming var is block-scoped like let
- Expecting ReferenceError outside block
- Confusing var with const or let scope
function test() {
if (true) {
var count = 5;
}
var count = 10;
return count;
}
console.log(test());What will be the output and why?
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]
- Thinking redeclaration causes error
- Assuming block scope limits var
- Expecting first value to be returned
Ready to prove it?
Pass the test and this pattern moves unknown
