1
var document = "temp";

function showDocument()
{
    alert(document); // temp or HTML DOM Document
}

It showed "HTML DOM Document" for me, which should be the case. Is it an object, reserved keyword, read-only object?

If it's an object, it should be editable. If it's a reserved keyword, JavaScript should throw an error/exception. If it's read-only, how can I modify the DOM itself which is accessible through document only?

1 Answer 1

5

It's a read-only property of the global (window) object. Its value is a native object representing the current HTML/XML/whatever document loaded into the window.

To modify the DOM, use DOM manipulation APIs.

edit — to clarify, the "document" property of the window object (or, if you prefer, the global variable called "document") is itself immutable, and its value cannot be changed by JavaScript. The value, however, is a reference to an object, an object that is mutable in all sorts of ways.

Sign up to request clarification or add additional context in comments.

2 Comments

If document is a read property, how does it happen that we say, document.getElementById('someDIV').style.background = 'red'; If document is read only, how am I able to execute this statement.
A read-only property (in this case, you also could say variable) pointing to an object does not mean that the object is not mutable. You can't modify the "pointer", but you can modify the DOM tree.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.