What is the difference between null and undefined?

This question already has an answer here:

  • What is the difference between null and undefined in JavaScript? 30 answers

  • This post explains the differences very good. They are the same in Typescript as in Javascript.

    As for what you should use: The Typescript coding styleguide itselft states that you should always use undefined and not null: Typescript Styleguide


    The value 'undefined' denotes that a variable has been declared, but hasn't been assigned any value. So, the value of the variable is 'undefined'.

    On the other hand, 'null' refers to a non-existent object, which basically means 'empty' or 'nothing'.

    You can manually assign the value 'undefined' to a variable, but that isn't recommended. So, 'null' is assigned to a variable to specify that the variable doesn't contain any value or is empty. But 'undefined' is used to check whether the variable has been assigned any value after declaration.

    链接地址: http://www.djcxy.com/p/76646.html

    上一篇: 在javascript中未定义vs null

    下一篇: null和undefined有什么区别?