MDN article on Node interface states that

interfaces [that inherit from Node interface] may return null in particular cases where the methods and properties are not relevant. They may throw an exception - for example when adding children to a node type for which no children can exist.

To me it seems like a strengthening of preconditions in a subtype - a violation of LSP.

Do these implementations actually violate LSP, or is my understanding incorrect?

2 Answers 2


Technically: no, this is not a violation of the LSP: The type Node gives us some guarantees here: Any method may work as documented below or return null or throw an exception. Any subtype of Node must still live by this contract.

In practice: yes, this is a questionable design. The DOM interface seems to use a variant of the Composite Pattern, and the design shows it's age. Today, it is not considered a best practice to have “optional methods” that don't have to be provided by a subtype. However, this does keep the number of types more manageable, and in statically typed languages removes the need for frequent casting – note that the DOM is not only used in JavaScript.


The documentation clearly states that interfaces which return null or throw an exception are behaving as expected.

Therefore such interfaces don't break the LSP, since they behave as expected. If the documentation said "interfaces never return null" then an interface returning null would violate the LSP. Since the documentation says the exact opposite, returning null is fine.

I think you need to get used to the fact that the LSP is intended to provide developers with something useful. We want different behaviour while having contracts fulfilled. I have the impression you would go to a DIY store and complain that they have paints in different colours, because paints in different colours violate the LSP. No, they don't. Do I complain to my user interface designer that replacing a green button with a blue button violates the LSP? No, I don't. They look different, but they can be substituted for each other. If the LSP didn't allow this, then frankly nobody would know what the letters mean and there would be no Wikipedia entry, because it would be useless.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.