What is the difference between null and undefined in JavaScript?

前端 未结 30 3252
夕颜
夕颜 2020-11-21 23:06

I want to know what the difference is between null and undefined in JavaScript.

30条回答
  •  北海茫月
    2020-11-21 23:34

    Quote from "Eloquent Javascript" 3rd edition by Marijn Haverbeke:

    The difference in meaning between undefined and null is an accident of Javascript's design, and it doesn't matter most of the time. In cases where you actually have to concern yourself with these values, I recommend treating them as mostly interchangeable

    Honestly, at first, I am a bit skeptical about this advice. However, in my own interpretation, it is a lazy (vs eager) way to deal with their differences. Maybe, we don't have to deal with the differences at all. If we have to, we can delay our concern (util we have to) and not hyperactively/defensively worry about it every step of the way as those values (null and undefined) flow through our code.

    PS: This is not a direct answer to your question. This is just a related opinion.

提交回复
热议问题