What is the difference in Javascript between 'undefined' and 'not defined'?

前端 未结 8 2205
半阙折子戏
半阙折子戏 2020-11-29 03:21

If you attempt to use a variable that does not exist and has not been declared, javascript will throw an error. var name is not defined, and the script will sto

8条回答
  •  自闭症患者
    2020-11-29 03:51

    An undeclared variable does not exist in Javascript memory whereas a declared one does and can be set to undefined.

    The difference is caused by Javascript design confusion. If a variable's value is undefined then it should be deleted. Indeed falsy values like 0, "", null, and undefined, are all basically meaningless and identical. A lot of time is wasted on them. They are all nothing and non existent. It is highly contradictory to have more than one type of nothing, or maybe to allow even one.

    Therefore I think you should avoid explicitly using falsy values when programming altogether. I didn't find a case yet when they couldn't be eliminated by a better method, and programming is much nicer without them.

提交回复
热议问题