What is the difference between null and undefined?
This question already has an answer here:
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下一篇: null和undefined有什么区别?