Accessing Undeclared Variables of a Java Scipt Object [duplicate] - javascript

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.

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

In Java Script Global Variable is not clear [duplicate]

This question already has answers here:
How can I unset a JavaScript variable?
(12 answers)
Closed 7 years ago.
I have declare global variable in java script, i try to clear with following code but it not clear.
As i defined global variable.
var usno;
then before set value i tried following code
delete window.usno;
usno = undefined;
usno=null;
but no success.
Please help on this
Thanks
Basit.
In short, you cannot delete variables defined with var. If you set the variable using window.usno then it can be. See Understanding delete and the answers to How to unset a JavaScript variable? for more details.
Do you really need to delete the variable though? Instead of making a global variable, restrict the scope of your variable to a function so that it will be garbage collected after the scope of the function ends.
Your first solution is the correct one.
delete window.usno;
That deletes the property usno from the global window object. But here is where the confusion lies, when we test it:
window.usno; // -> undefined
So, a deleted or non-existent property returns undefined. Thus, in effect, the following two statements achieve much the same thing:
delete window.usno;
window.usno = undefined;
They are not exactly the same thing. In the second case, the usno property still exists, as you will find if your iterated through all of the properties of window.
Short answer:
delete window.usno;

Variables to define nested objects? [duplicate]

This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 7 years ago.
I have an object and a few variables I'd like nested inside the variable but for some reason I can only ever get the first nest. Anything after that gives me an error stating that it couldn't read because it was undefined:
var date = 10;
var timestamp = 100;
Que[date] = {timestamp:{"test":"test"}};//this returns {10:{"timestamp":{"test":"test"}}}; for some reason
Que[date][timestamp] = {"test":"test"}; //errors saying, cannot read '100' undefined
console.log(Que);
I'm not sure why this is happening and I'd really like to resolve it with simple means. Btw the Que variable is global inside another script and predeterminietly contains {"10":{"24":{"1":"test"}}}; which is likely why the date variable does work but the timestamp variable doesn't. Any suggestions?
EDIT:
date, and timestamp are both declared outside of the object, however i wish to use them as key's inside of the object..
uppon reading the mod suggested post and implementing what it contained, i ended up with another error
Que = {[date]:{[timestamp]:{"test":"test"}}};//this results in an unexpectd token error located at the [ before date
I think there are more questions to be asked based off of your code snippet, but in your example timesheet is not defined, you can reference the key inside your JSON by using
Que[date]["timesheet"]
To access the object inside. The key that you're looking up with the [] notation should be a string.

Do 'variables' have properties? [duplicate]

This question already has answers here:
Why can't I add properties to a string object in javascript?
(2 answers)
Closed 9 years ago.
Do variables have properties?
The obvious answer should be NO. If I try to assign a property to a variable, it should give an error. Right?
If I do something like:
var someVariable = 'Cat';
someVariable.eyes = 'two'; //Gives no error!
alert(someVariable.eyes); // alerts 'undefined' instead of giving an error!
Variables don't have properties, but their values do. (If the value's an object, anyway.)
In this case, you're trying to set the eyes property of the string currently referenced by someVariable.
It won't work in this case, though. Since primitive values don't have properties, JS will convert the primitive string value into an object and set the property on that object, which pretty much immediately gets silently discarded. The end result: the primitive string remains unmodified.
"Variables" don't actually exist (except strictly within the definition of a scope), only objects. And string objects can't have arbitrary properties assigned by default.

Categories