What will the value of Var be in the following code snippet?var Var='var';var Var;
Question
What will the value of Var
be in the following code snippet?
var Var='var';
var Var;
Solution
In JavaScript, when you declare a variable with var
, it gets hoisted to the top of its scope, but its assignment happens where it appears in the code.
- Variable Declaration: The line
var Var='var';
declares a variableVar
and immediately assigns it the string'var'
. - Second Declaration: The line
var Var;
is a second declaration of the same variableVar
. Due to hoisting, this does not change the value that was assigned earlier.
After executing both lines, the variable Var
will still hold the value assigned to it in the first line.
Final Answer
The value of Var
will be 'var'
.
Similar Questions
What is the output of following code?var a = 10;function test() { a = 20;}test();console.log(a);Options: Pick one correct answer from below1020
Which of the following is the correct way to declare a variable in JavaScript?variable name = "John";var name = "John";let name == "John";name: "John";
What is the correct way to declare a variable in JavaScript?Points:1All of the abovevar myVariable = 10;I don't knowlet myVariable = 10;const myVariable = 10;
What will be the value of $var?$var = 1 / 2;Question 20Select one:a.0b.0.5c.1d.2
What is the correct syntax to declare a variable in JavaScript?var myVariable;variable myVariable;Both a and clet myVariable;
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.