This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 4 years ago.
What is the difference between the following two cases:
CASE 1:
console.log(c); //console : undefined
var c;
CASE 2:
console.log(c); //console : Uncaught ReferenceError: c is not defined
Why in Case 1 it console undefined? as it was declare after console.log(). At that time variable c is also not defined than why program console undefined
Please check docs for var statement in MDN
variable declarations (and declarations in general) are processed before any code is executed, declaring a variable anywhere in the code is equivalent to declaring it at the top. This also means that a variable can appear to be used before it's declared. This behavior is called "hoisting", as it appears that the variable declaration is moved to the top of the function or global code.
So, in first example variable has been declared, but was not defined (even with var c = 1; before that row it would be not defined). In the second example, it was not declared at all.
Related
This question already has answers here:
Using Javascript hoisting concept , I tried the following. I am not sure why the name variable has been picked up but not the age [duplicate]
(1 answer)
What is the global variable called 'name' in javascript? [duplicate]
(2 answers)
Closed last month.
I am trying to understand in a more deep way hoisting, creation phase and execution phase in JavaScript. To the best of my knowledge variables declared with var keyword are hoisted to the top of their scope. But I am running into this problem.
console.log(name); // Output -> 'any name'
console.log(age); // Output -> undefined
var name = 'any name';
var age = 30
I know at the moment when console.log statements are executed bot variables should be undefined because they are declared but values have not been assigned to them (they got undefined in the creation phase). However browsers keep giving me one of the values as undefined (as I expect it to be), but with name variable it is reading the string. Any ideas as to why is this happening?
This question already has answers here:
Are variables declared with let or const hoisted?
(7 answers)
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Executing an undeclared variable x gives error: Uncaught ReferenceError: x is not defined
Accessing a variable before declaration with let gives similar error: Uncaught ReferenceError: y is not defined
console.log(y)
y=10;
let y;
As we see that both cases throws similar error but we know that. So, how to prove that variable hoisting do happen with let or const declaration?
I know just one way to prove that variable hoisting do happen with let or const despite of same Uncaught ReferenceError.
if variable is hoisted in let or const declaration, the variable must have been declared, that means it has already allocated a memory space
if we can prove that the variable has occupied a memory space before accessing it (rather before code execution), that means the variable is hoisted
We can use a browser dev-tools to check for the same, let us add a debugger in the first line to pause the execution before accessing the variable declared with let
debugger;
console.log(y)
y=10;
let y;
When this is executed in console tab, it redirects to the source tab where we can see the variable y already allocated a memory space that's why it is shown under Script in the Scope section on the right side (denoting lexical scope of let declaration for variable y)
However, this will throw the same error: Uncaught ReferenceError: y is not defined in line 2, since it is not defined.
But the point is we have proved that the variable has allocated a memory space before execution, hence variable hoisting do occur in let or const declaration
Let me know your views. I would like to know more ways to prove the same. So please feel free to add your points.
This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Why is no ReferenceError being thrown if a variable is used before it’s declared?
(3 answers)
Closed 5 years ago.
I wonder why the two code snipped below generate two different results as in reference below the two have to be the same as were are in global scope and placing var would not make a difference.
I was under the impression due to what explained below and the Javascript hoisting the two generate same output, but don't! why?
What is the purpose of the var keyword and when to use it (or omit it)?
console.log(a)
var a = 90;
// undefined
vs
console.log(a)
a = 90; // no var keyword
// Uncaught ReferenceError: a is not defined
When you put var keyword, what it does is it moves the var part as a declaration on that scope and makes it undefined. In the first case, with var:
console.log(a)
var a = 90;
// undefined
Gets rewritten as:
var a = undefined;
console.log(a)
a = 90;
// undefined
So, you get the undefined here, vs. your second case has it the same way, where there's no declaration or key in window object named a. Hope this is understandable.
The point here is, when you use a var keyword, immediately that's defined as undefined at the top of the scope before anything.
This question already has answers here:
Javascript function scoping and hoisting
(18 answers)
Closed 6 years ago.
Case 1 - If I console.log(variable) before the variable declaration I get undefined. eg;
// code
console.log(a);
var a ;
// output
undefined
Case 2 - If I console.log(variable) without variable declaration I get Uncaught ReferenceError: variable is not defined.
// code
console.log(a);
// output
Uncaught ReferenceError: a is not defined
But incase of functions we can call a function before or after function definition it never give any issue. eg;
console.log(example());
function example(){
return 'test done';
}
console.log(example());
// output without any issue
Now My question is, what is the difference between undefined and not defined.
Undefined means - variable exists, but have no any stored value in it.
Not defined means - variable not declared (not exist).
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 8 years ago.
I just encountered this variable declaration syntax for the first time.
var myVar = (function() {
return 1;
})();
My 2 main questions are..
What is it called, and why is it legal?
Forgive me if this question has been asked before, I tried searching around but I have no idea what this notation is called so I wasn't able to find anything.
Also, I should add, what is the function of the 2 sets of parentheses? The first of which encloses the function, the second of which is empty.
Self executing functions are typically used to encapsulate context and avoid name collusions. Any variable that you define inside the (function(){..})() are not global.
The following code:
var same_name = 1;
var myVar = (function() {
var same_name = 2;
console.log(same_name);
})();
console.log(same_name);
produces this output:
1
2
By using this syntax you avoid colliding with global variables declared elsewhere in you javascript code.
I am not sure what this is called, other than defining an anonymous function and immediately invoking it.
It is perfectly legal, because
Defining an anonymous function is legal.
Invoking it and assigning the return value is also legal.
The end result is that myVar = 1.
This is an anonymous function (also called lambda function) that is being executed immediately with its return value (1) being assigned to a variable (myVar). It's legal because the specification says it is. It is a very common feature in many languages.