Prop Document Nodename
## XML DOM nodeName Property
The `nodeName` property returns the name of a node, depending on its node type. For a `Document` node, this property always returns a specific, pre-defined string.
---
## Definition and Usage
In the XML DOM (Document Object Model), every part of an XML document is a node. The `nodeName` property is a read-only property that provides the name of the selected node.
When accessed on a **Document object** (the root of the XML document tree), `nodeName` always returns the string **`"#document"`**.
### Node Names by Type
To understand how `nodeName` behaves across different node types, refer to the table below:
| Node Type | `nodeName` Return Value |
| :--- | :--- |
| **Document** | `"#document"` |
| Element | The tag name (e.g., `"book"`, `"title"`) |
| Attribute | The attribute name (e.g., `"category"`) |
| Text | `"#text"` |
| Comment | `"#comment"` |
---
## Syntax
```javascript
documentObject.nodeName
```
### Return Value
* **Type:** String
* **Value:** `"#document"` (when called on a Document object).
---
## Code Example
The following example loads an XML file named [books.xml](/try/demo_source/books.xml) using the helper function `loadXMLDoc()` and retrieves both the `nodeName` and `nodeType` of the document node.
```javascript
// Load the XML document
xmlDoc = loadXMLDoc("books.xml");
// Output the node name and node type of the Document object
document.write("Nodename: " + xmlDoc.nodeName);
document.write(" (nodetype: " + xmlDoc.nodeType + ")");
```
### Output
```text
Nodename: #document (nodetype: 9)
```
---
## Practical Considerations
1. **Read-Only:** The `nodeName` property is read-only. Attempting to assign a new value to it will fail or be ignored.
2. **Case Sensitivity:**
* In **XML**, `nodeName` preserves the exact case of the elements as defined in the source file.
* In **HTML DOM**, `nodeName` on element nodes typically returns the tag name in uppercase (e.g., `"DIV"`, `"BODY"`), regardless of how it was written in the HTML source. However, for the `Document` node itself, it always returns lowercase `"#document"`.
3. **Checking Node Types:** While you can use `nodeName` to check if a node is a document (by checking if `node.nodeName === "#document"`), it is generally considered best practice to use the `nodeType` property (which returns `9` for `Document` nodes) for type-checking logic, as numeric comparisons are faster and less prone to casing issues.
YouTip