What is the difference between null and undefined in JavaScript?

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

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

30条回答
  •  悲哀的现实
    2020-11-21 23:51

    In javascript all variables are stored as key value pairs. Each variable is stored as variable_name : variable_value/reference.

    undefined means a variable has been given a space in memory, but no value is assigned to it. As a best practice, you should not use this type as an assignment.

    In that case how to denote when you want a variable to be without value at a later point in the code? You can use the type null ,which is also a type that is used to define the same thing, absence of a value, but it is not the same as undefined, as in this case you actually have the value in memory. That value is null

    Both are similar but usage and meaning are different.

提交回复
热议问题