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

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

Related

why global variable returns undefined when called in a function defined and invoked in global scope as this.variable [duplicate]

This question already has answers here:
How does the "this" keyword work, and when should it be used?
(22 answers)
Do let statements create properties on the global object?
(5 answers)
Closed 3 months ago.
I am a noob to programming and i am trying to understand 'this' keyword. so i defined a variable in global scope and i was expecting that if i use such a variable with 'this' keyword in a function defined and called in global scope, function would return value of the variable which was assinged in global scope. but i am getting undefined
`
let title = "global";
const func = () => {
const title = "func";
console.log(this);
console.log(this.title);
};
func();
`
console show window object and undefined

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.

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.

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

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.

Categories

Resources