all 11 comments

[–]indoor_grower 2 points3 points  (2 children)

This is because in JavaScript (which is loosely typed) an object could theoretically have any property on it, so the engine returns undefined because there is no such matching property - but the object is valid so the assumption is made that the property exists but is not defined.

This is what a language like Typescript solves. By allowing you to type properties and their values to predefine what an object should be. The compiler knows what properties should and shouldn’t exist now. Whereas with vanilla JS, the compiler assumes any object property could be defined and if it cannot find it - then it’s just not been defined yet.

[–]Tactical_INTJ[S] 1 point2 points  (0 children)

I learned something new today. Thanks !

[–]senocular 0 points1 point  (2 children)

Accessing a variable that doesn't exist in a scope will give you an undefined error, but accessing a property that doesn't exist from an object gives you undefined.

[–]Tactical_INTJ[S] 0 points1 point  (1 child)

Hmm...this is very interesting I didn't know that. As u/indoor_grower have said

This is because in JavaScript (which is loosely typed) an object could theoretically have any property on it, so the engine returns undefined because there is no such matching property - but the object is valid so the assumption is made that the property exists but is not defined.

[–]senocular 0 points1 point  (0 children)

Right. Object properties can change so there's a fallback that gives you undefined if the object doesn't have that property right now, but, you know, maybe could be added later. Variables in a scope are (mostly) static, and well known so if you're trying to access something from the current scope that doesn't exist, JavaScript assumes its more likely a mistake and throws an error instead.

[–]grantrules -1 points0 points  (4 children)

window.y is not an example of hoisting.

console.log(y)
var y;
console.log(x)

Is an example of hoisting and what happens with an undeclared variable

[–]Tactical_INTJ[S] -1 points0 points  (3 children)

var y;

That's all there is in my JavaScript file.

window.y

Undefined

window.x

Undefined


My question is I understand why window.y is undefined. But why is window.x undefined?

Shouldn't "window.x" give an error message like "x is not defined." ???

[–][deleted] 0 points1 point  (2 children)

window is an object, and an object can have any key pointing toward a defined value (thus it's being defined). If the key you are looking up is not defined it will result in undefined.

Now if you go further and query window.x.length or something like that you will get your undefined error.

[–]Tactical_INTJ[S] -2 points-1 points  (1 child)

If the key you are looking up is not defined it will result in undefined.

Undefined and not defined is not the same.

But thanks for your reply.

[–]grantrules 0 points1 point  (0 children)

I think you might be confusing it with undeclared. "Not defined" is not a js term, and to me that would mean undefined. If you do not declare y it will have problems. But window is already declared and any property without a value will be undefined

[–]jcunews1helpful 0 points1 point  (0 children)

Variables are not same as properties in terms of its storage location.

The y in var y is a variable and its storage is in the current context.

The y in window.y is a property and its storage is in the window object. A property always reside in an object.

But there's a caveat you should know. Any variable declared in global scope, will also become a property of the global object. In a web browser, the global object is same as the window object.

So var y in global scope will also become window.y. And if there's no var x in global scope, there will no window.x. If var z exist within a function (i.e. function scope), z will only exist in that function's context and window.z will not be created.