Each has its own coding method and its own internal semantics, but over the years I have found this to be the most intuitive advice that I give to people who ask this question: when in doubt, do what JavaScript does .
Suppose you are working with object properties, such as jQuery plugin options ... ask yourself what JavaScript value gives a property that is not yet defined - the answer is undefined . Therefore, in this context, I would initialize these types of things with undefined so that it matches JavaScript (for variables you can do var myVar; instead of var myVar = undefined; ).
Now let's say that you are manipulating the DOM ... what value does JavaScript assign to non-existent elements? The answer is null . This is the value that I would initialize if you create a placeholder variable that will later contain a link to an element, document fragment or similar information related to the DOM.
If you are working with JSON, you need to make a special case: for undefined property values, you must either set them to "" or null , because an undefined value is not considered the correct JSON format.
With that said, as the previous poster put it, if you find that you initialize the material using null or undefined more than once in the blue moon, then you might want to reconsider how you are going to code your application.
thdoan Oct 28 '15 at 17:29 2015-10-28 17:29
source share