Hoisted JavaScript variables undefined or not? [duplicate] - javascript

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?

Related

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

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.

Accessing Undeclared Variables of a Java Scipt Object [duplicate]

This question already has an answer here:
why referencing non-existent property of an object in javascript doesn't return a reference error?
(1 answer)
Closed 5 years ago.
I was playing with variables, when i encountered this. I could not find out a reason for this. I want to understand this.
Below gives an exception for reference error which is obvious:
console.log(age);
but when the same thing is done on an object, it does not throw any kind of error. It prints out 'undefined' as if the variable is already declared:
var person = {};
console.log(person.age)
And interestingly when you check the 'person' object, there is no 'age' property.
I understand we can create property on objects directly like this:
person.age = 3;
and so can be done for global or local variables:
a = 3
But still, accessing something before assigning or declaring should throw an exception or error just like it happens in case of global or local scope variables.
Below gives an exception for reference error which is obvious
Yes, because there is no reference age.
but when the same thing is done on an object, it does not throw any
kind of error.
Why would it? You're referencing an object through valid reference person, and since this object doesn't have the property age the value undefined is returned.

Parameters pass by value vs parameters as local variables in Javascript [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Closed 6 years ago.
Say for example you have a function like below
var value = 1;
function test(myVar) {
myVar = 2
}
test(value)
console.log(value)
//prints out 1
Now, I have read that there are two things that happen in JS functions.
1) Javascript passes primitives by value. When "value" is passed as an argument to test(), a copy is created that is separate from the global variable "value". Therefore the "myVar" is a separate variable from "value"
2) Parameters in JS functions are really just local variables to the function. So, the "myVar" parameter has scope only inside of the test() function.
I know both statements are true, but which one of these statements causes console.log(value) to print out 1
Both of those statements are true. The reason why it prints out 1 is that, as you mentioned, parameters are passed by value. As such, the value of your variable value is never altered.
Although point #2 is also true, it doesn't really have much to do with this behaviour. it is more related to doing something like using myVar outside of that function's scope.
Both of the statements are correct
Please check this URL from Mozilla Variable Scope in Javascript

javascript variable's weird behaviour [duplicate]

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

Categories