'var a' vs 'this.a' in function block [duplicate] - javascript

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
What is the scope of variables in JavaScript?
(27 answers)
Closed 5 years ago.
I tried to search it on internet but didn't got any answer. Why access of two variables before declaration have two different outputs:
function test() {
console.log(a); // prints 'undefined'.
console.log(b); // prints 'b is not defined'.
var a = 5;
this.b = 10;
}

when you declare var variable inside the function then that variable is limited to that function. That mean scope of that variable is limited to that function so you can't access that variable outside of that function.
But if you use this then it can be access through outside but then again you need to create a object of that class/function and access trough it.
function test() {
var a = 5;
this.b = 10;
this.printB = function(){
return this.b;
}
this.printA = function(){
return a;
}
}
var obj = new test();
console.log(obj.a)
console.log(obj.b)
console.log(obj.printA())
console.log(obj.printB())

when you write a variable with var like var a it become a function
level variable. so now a is a variable which is present there before consol.log(a) and its not given a value so its undefined for b there is no variable defined with name b before it called.
And which all variables are defined with var will be function level variable and will be created before anything else in the function. so a will be present in the function at the starting itself.
and here this will be holding the object of Windows.

First of all, you are trying to examine the variables before you declared them.
This is the correct order:
function test()
{
var a=5;
this.b=10;
console.log(a); // Should print 5
console.log(b); // Should print undefined
console.log(this.b); // Should print 10
}
Second, regarding your question, declaring a var inside your function, make is a local private variable inside your function.
declaring this.b, will cause b to be in a different scope, and b for itself as undefined. If you want to use b inside the function, use this.b.

Related

Javascript - Defining a local variable after assigning a new value in a already declared global variable is overriding the global variable. Why? [duplicate]

This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 1 year ago.
I am learning javascript global variable and local variable. When I did experiment with it (see jsfiddle below) then I am confuse that why after executing myFunction1() the value of variable a became 4 instead of 5. What do you think happened inside the javascript memory which made its value 4 instead of 5 ?
var a = 4;
myFunction1();
function myFunction1() {
a= 5;
var a= 6;
}
alert(a); //a = 4
myFunction2();
function myFunction2() {
a= 5;
}
alert(a); //a = 5
You've just discovered hoisting.
By declaring var a inside the function, it is available to the whole function context (even before its declaration), so when you write a = 5 the actual a is not the one you expect, it's the function-scoped a.
In the second case, a is the one you expect (outside the function)

Scoped Variables : Explanation for the code ouput? [duplicate]

This question already has answers here:
Surprised that global variable has undefined value in JavaScript
(6 answers)
Closed 2 years ago.
Why is the output of the following snippet undefined, 5?
var a=5;
(function(){
console.log(a);
var a= b = 10;
})();
console.log(a);
There is something called hoisting in javascript. Hoisting means that all the variables which are declared inside a scope a brought to the top of the scope and declared there and then assigned on their corresponding line.
The above code is same as
var a=5;
(function(){
var a;
console.log(a);
a= b = 10;
})();
console.log(a);
In the above function when var a; is declared but not initialized so it will get assignment to undefined automatically. Hence you see undefined in the first log.
And in the second log you see 5 because the previous variable a is inside that function and not available outside. In the outside log the a in global scope will be logged.

Why this global variable is undefined inside a function? [duplicate]

This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
'Hoisted' JavaScript Variables
(7 answers)
Why a variable defined global is undefined? [duplicate]
(3 answers)
Closed 5 years ago.
Why is this global variable undefined inside a function if the same global variable is re-declared and defined inside that same function?
var a = 1;
function testscope(){
console.log(a, 'inside func');
//var a=2;
};
testscope();
console.log(a, 'outside func');
output:
1 "inside func"
1 "outside func"
Consider same code where var a = 2; inside function block is uncommented
var a = 1;
function testscope(){
console.log(a, 'inside func');
var a=2;
};
testscope();
console.log(a, 'outside func');
Output
undefined "inside func"
1 "outside func"
It's because Javascript is not like Java and variable declaration are always pushed up their block. Your second piece of code is strictly equivalent to:
var a = 1;
function testscope(){
var a; // <-- When executed, the declaration goes up here
console.log(a, 'inside func');
a=2; // <-- and assignation stays there
};
testscope();
console.log(a, 'outside func');
Output
undefined "inside func"
1 "outside func"
because a on the first function refers to the variable a that exists on the function, while a you write after the variable you write. if you want a global variable accessible inside a function that contains the same variable as the global variable you should add this.a. or if you want to access the variable a in the function you have to write the variable before you call it

what is the difference between these two a = 'abc'; var b = 'abc'; [duplicate]

This question already has answers here:
What is the purpose of the var keyword and when should I use it (or omit it)?
(19 answers)
Closed 5 years ago.
The difference between these two lines of code?
a = 'abc';
var b = 'abc';
Are they just different variables? is that it?
I want to say it is but I am just learning.
The first one implicitly creates a global variable and the second one creates a variable in the current scope.
It depends.
On global scope wise there is no difference. However, if you are on local scope there is a difference.
//Both global
var test1=1;
test2=2;
function first()
{
var test1 =-1; // Local: set a new variable independent of the global test1
test2 =3; // Change the test2 global variable to 2
console.log(test1); //will display -1 (local variable value)
}
function second()
{
console.log(test1); //will display 1 (global variable value)
}
Inside function first() the value of test1 is -1 because we test1 is hitting the local variable created using var, function second() has no test1 as a local variable so it will display 1.

Why don't parameters retain their value outside of a function? [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
The following JS code behaves oddly:
var c = 0;
var a = 0;
myTest(8, 5);
function myTest(a,b) {
console.log(a);
a++;
c++;
console.log(a);
return a;
}
console.log(a);
console.log(c);
https://jsfiddle.net/hwns1v4L/
If I take the "a" parameter out of the function, "a" increments by 1 and the third console log returns "a" as 1. Totally normal. But if I keep the "a" as a parameter in myTest function (as in the code above), it gets a value of 8, increments by 1 to 9, and the third console log returns a zero.
What is the explanation for this weird behavior? I am sorry if this is explained in another thread; I am too new for JS to produce really good google queries or understand advanced answers.
What is the explanation for this weird behavior?
Because in Javascript variables are function scoped.
You never passed a and b to myTest method. You passed 8 and 5, so a and b which were part of myTest signature got new scope. a became 8 and b became 5 inside myTest.
Values of a and b inside myTest will not be used outside since their scope is limited to myTest.
Inside your function, you have a local a parameter. So any changes you make to that value, they will not reflect your globally defined a. Since you did not create a c variable or parameter inside the function, you will be changing the global c value.
var c = 0; // Global c
var a = 0; // Global a
var b = myTest(8, 5); // Store the value of the local a from the function return.
function myTest(a,b) {
console.log(a); // This a is a local reference (8)
a++; // Increment local a
c++; // Increment global c
console.log(a); // Print local a (9)
return a; // Return local a
}
console.log(a); // Print global a (0)
console.log(c); // Print global c (1)
console.log(b); // Print returned value (9)

Categories

Resources