0

I've been wondering about the use of the keyword null in the JS language. In cases like C/C++, NULL is stood to mean something like a null pointer, i.e. a pointer to 0 (though there are macro definitions, etc, which can change this), but how is JS specified to handle the keyword? Is there some specification that says how it should be processed?

  • Look this post, might be useful http://stackoverflow.com/questions/5515310/is-there-a-standard-function-to-check-for-null-undefined-or-blank-variables-in – KodornaRocks Apr 23 '15 at 05:26
  • Does this have any impact on how you'd use it? Possibly related to http://stackoverflow.com/questions/801032/why-is-null-an-object-and-whats-the-difference-between-null-and-undefined – Krease Apr 23 '15 at 05:27
  • @DiogoPaim It's a good reference, but it doesn't quite answer how the JS interpreter actually handles the keyword. – Guillermo Angeris Apr 23 '15 at 05:28
  • @Chris Nah, it's not so much the usage, per se, but it's pretty useful to know how things are being handled at the lower level. – Guillermo Angeris Apr 23 '15 at 05:28
  • 1
    The specification is [ECMA-262](http://www.ecma-international.org/publications/standards/Ecma-262.htm), but implementations may vary. In particular ECMA-262 doesn't seem to say anything more than "The null value represents the intentional absence of any object value." Someone with more intimate knowledge of the spec might have to correct me on that one though. – BoltClock Apr 23 '15 at 05:35
  • @BoltClock Huh, that's interesting. But yeah, it doesn't seem to mention anything apart from the obvious `null` should be *null*. Is it interpreter-dependent, then? – Guillermo Angeris Apr 23 '15 at 05:58
  • @GuillermoAngeris if the standard does not require any particular implementation - it's up to implementation how to manage it. – zerkms Apr 23 '15 at 06:15
  • @zerkms Well, yes. So, then, I guess comes the question: how is it generally handled? – Guillermo Angeris Apr 23 '15 at 06:17
  • 1
    http://izs.me/v8-docs/namespacev8.html#aa6bb9749edb4ef25314964762bc4d5e8 ? – zerkms Apr 23 '15 at 06:41

1 Answers1

0

null is a literal in javascript which indicates an empty value or an empty object. This should not be confused however, with undefined which is the value of an uninitialized variable or object. Moreover, care has to be taken always when using the equality operator (==) or the identity operator (===) on null and undefined values, as the comparison gives totally different result depending on what operator is used:

From the Mozilla Developer Docs:

typeof null        // object (bug in ECMAScript, should be null)
typeof undefined   // undefined
null === undefined // false
null  == undefined // true
Prahlad Yeri
  • 3,567
  • 4
  • 25
  • 55