JavaScript `undefined` vs `void 0`

前端 未结 4 1159
一个人的身影
一个人的身影 2020-11-28 08:52

What exactly is the difference between undefined and void 0 ?

Which is preferred and why?

4条回答
  •  眼角桃花
    2020-11-28 09:48

    Parentheses here are optional, void 0, void(0) and void (0) are equivalent. The void is a unary operator with a right-to-left associativity, hence the value is placed at the right of it:

    void .

    For second question, you need to use undefined directly while avoiding unneeded operand evaluation to retrieve the same undefined value.

    More info in the reference: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/void

提交回复
热议问题