What reason is there to use null instead of undefined in JavaScript?

前端 未结 14 858
情书的邮戳
情书的邮戳 2020-11-30 20:28

I\'ve been writing JavaScript for quite a long time now, and I have never had a reason to use null. It seems that undefined is always preferable an

14条回答
  •  星月不相逢
    2020-11-30 21:20

    DOM nodes and elements are not undefined, but may be null.

    • The nextSibling of the last child of an element is null.

    • The previousSibling of the first child is null.

    • A document.getElementById reference is null if the element does not exist in the document.

    But in none of these cases is the value undefined; there just is no node there.

提交回复
热议问题