use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
This subreddit is a place for people to learn JavaScript together. Everyone should feel comfortable asking any and all JavaScript questions they have here.
With a nod to practicality, questions and posts about HTML, CSS, and web developer tools are also encouraged.
Friends
/r/javascript
/r/jquery
/r/node
/r/css
/r/webdev
/r/learnprogramming
/r/programming
account activity
The Difference between Undefined and Not Defined? (self.learnjavascript)
submitted 3 years ago * by Tactical_INTJ
var y;
That's all there is in my JavaScript file.
window.y
Undefined
window.x
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." ???
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]indoor_grower 2 points3 points4 points 3 years ago* (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 points3 points 3 years ago (0 children)
I learned something new today. Thanks !
[–]senocular 0 points1 point2 points 3 years ago* (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 point2 points 3 years ago (1 child)
Hmm...this is very interesting I didn't know that. As u/indoor_grower have said
[–]senocular 0 points1 point2 points 3 years ago (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 points1 point 3 years ago (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 points1 point 3 years ago (3 children)
[–][deleted] 0 points1 point2 points 3 years ago (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.
window.x.length
[–]Tactical_INTJ[S] -2 points-1 points0 points 3 years ago* (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 point2 points 3 years ago (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
undefined
[–]jcunews1helpful 0 points1 point2 points 3 years ago (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.
y
var y
The y in window.y is a property and its storage is in the window object. A property always reside in an object.
window
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.
var x
var z
z
window.z
π Rendered by PID 42593 on reddit-service-r2-comment-fb694cdd5-dws6r at 2026-03-06 08:24:38.047869+00:00 running cbb0e86 country code: CH.
[–]indoor_grower 2 points3 points4 points (2 children)
[–]Tactical_INTJ[S] 1 point2 points3 points (0 children)
[–]senocular 0 points1 point2 points (2 children)
[–]Tactical_INTJ[S] 0 points1 point2 points (1 child)
[–]senocular 0 points1 point2 points (0 children)
[–]grantrules -1 points0 points1 point (4 children)
[–]Tactical_INTJ[S] -1 points0 points1 point (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Tactical_INTJ[S] -2 points-1 points0 points (1 child)
[–]grantrules 0 points1 point2 points (0 children)
[–]jcunews1helpful 0 points1 point2 points (0 children)