0
0
Javascriptprogramming~5 mins

Variable declaration using var in Javascript - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the var keyword do in JavaScript?

The var keyword declares a variable that can be used throughout its function or globally if declared outside a function.

Click to reveal answer
beginner
What is the scope of a variable declared with var inside a function?

It has function scope, meaning it is accessible anywhere inside the function where it is declared.

Click to reveal answer
beginner
Can you redeclare a variable using var in the same scope?

Yes, variables declared with var can be redeclared in the same scope without error.

Click to reveal answer
intermediate
What happens if you declare a var variable inside a block like if or for?

The variable is not block-scoped but function-scoped, so it is accessible outside the block within the function.

Click to reveal answer
intermediate
What is hoisting in relation to var variables?

Hoisting means the variable declaration is moved to the top of its scope before code runs, so you can use the variable before its declaration but its value will be undefined until assigned.

Click to reveal answer
What is the scope of a variable declared with var inside a function?
AGlobal scope only
BFunction scope
CBlock scope
DNo scope
What happens if you redeclare a var variable in the same scope?
AIt overwrites the previous variable
BIt causes an error
CIt creates a new variable with a different name
DIt is ignored
Which of these is true about var variables and hoisting?
ABoth declaration and assignment are hoisted
BOnly the assignment is hoisted
CNeither declaration nor assignment is hoisted
DOnly the declaration is hoisted, not the assignment
If you declare a var variable inside an if block, where can you access it?
AOnly outside the <code>if</code> block
BOnly inside the <code>if</code> block
CAnywhere inside the function containing the <code>if</code> block
DNowhere
Which keyword introduced in ES6 provides block scope, unlike var?
Alet
Bconst
Cvar
Dfunction
Explain how var variables behave with respect to scope and hoisting.
Think about where the variable is accessible and what happens if you use it before declaring.
You got /3 concepts.
    Describe the difference between var and let in terms of scope.
    Consider how variables behave inside blocks like if or for.
    You got /3 concepts.