XMLHttpRequest.DONE: why do we use it?

I’m making my way through the JavaScript Requests coursework and I have a question about the boilerplate XHR code shown in the supplied diagram:

https://s3.amazonaws.com/codecademy-content/courses/intermediate-javascript-requests/diagrams/XHR+POST+diagram.svg

In the anonymous function attached to the xhr.onreadystatechange event, we have an if statement to check whether our request has a readyState equal to XMLHttpRequest.DONE.

From everything I’ve read XMLHttpRequest.DONE === 4, so why do we rely on this way of “getting to 4”, rather than just using 4?

I also can’t seem to find any documentation anywhere about how the .DONE property of the XMLHttpRequest works, why it’s capitalised, what other ones are available, etc.

Thanks for any input!


This link will shed some light on the topic. As far as why not just use 4? You could, and it will work just fine. I suppose the purpose of using the XMLHttpRequest.DONE syntax in the lesson is to illustrate its use and meaning.

Thanks @midlindner, but that doc doesn’t explain how one would find XMLHttpRequest.DONE in the first place. It does explain that you might expect one of those five values to be returned by XMLHttpRequest.readyState but nowhere does it mention the constant property of .DONE with which you can compare the state.

Ok, try this link:
https://xhr.spec.whatwg.org/#xmlhttprequest
Not sure if it will answer your questions, but this is the actual XMLHttpRequest Standard.
Hope it helps!

That helps a lot, thank you!