javascript variable's weird behaviour [duplicate] - javascript

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).

Related

Hoisted JavaScript variables undefined or not? [duplicate]

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?

Why is variable undefined in global object even though it has not been defined? [duplicate]

This question already has answers here:
ReferenceError and the global object
(3 answers)
Closed 1 year ago.
I have this code:
console.log(apple);
If I run the program, I get the following result:
console.log(apple);
^
ReferenceError: apple is not defined
This is what I expected, but if I modify the code to print the apple variable from the global object, I get undefined
console.log(global.apple);
Result:
undefined
How is this undefined? As per my understanding, this should give the ReferenceError too right? Sorry if this is a simple concept, I am trying to understand the fundamentals. Any reference would be helpful.
What you're forbidden to do is reference a standalone identifier that the interpreter cannot resolve.
Referencing a property of an object - even if the property doesn't exist - is perfectly fine. It will result in undefined, but it won't throw.
const obj = {};
console.log(obj.foo);

Is this JS bug? [duplicate]

This question already has answers here:
What causes the different behaviors between "var" and "let" when assign them a returned value of a function which throws an error
(1 answer)
Is there any difference between declared and defined variable
(1 answer)
Closed 3 years ago.
I tried to run this code.
let a = b; //I'm sure that variable b is not defined.
Uncaught ReferenceError: b is not defined
I know this code causes this error.
b = 3;
Uncaught ReferenceError: b is not defined
let b = 3
Identifier 'b' has already been declared
Now, I can't put any String , Number,Null and Everything.
And I can't define variable 'b'.
Is this Js Bug?

What if we console a variable just before declare it [duplicate]

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.

How do we explain javascript hoisting here? [duplicate]

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.

Categories