What are the use cases of null in java-script?When null Should be used instead of undefined?
-
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/null – Pranav C Balan May 15 '16 at 08:54
-
A duplicate of http://stackoverflow.com/questions/5076944/what-is-the-difference-between-null-and-undefined-in-javascript – ivan_pozdeev May 15 '16 at 09:48
1 Answers
This is a really broad question. You can/should use null whenever you want something that looks like an object reference but has no value. null is the object-reference-type version of undefined (or undefinedis the non-object-reference-type version of null).
There are a couple of places you may have to use null rather than undefined or something else: When dealing with host-provided objects that expect an object reference. Say we have a host-provided object foo that has a property thingy that may have a reference to another host-provided object. Because the host environment isn't required to make its objects full JavaScript objects, you may find that you get an error assigning a non-object-reference-type value like undefined to foo.thingy where assigning null to it works. (That said, most host environments will apply type coercion in that situation, and a large number of them now provide true JavaScript objects rather than fake ones.)
- 1,031,962
- 187
- 1,923
- 1,875