-4

What are the use cases of null in java-script?When null Should be used instead of undefined?

Naqeeb Sial
  • 627
  • 6
  • 11

1 Answers1

5

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.)

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639