Questions And Answers

More Tutorials

Javascript Null and Undefined

The differences between null and undefined

null and undefined share abstract equality == but not strict equality ===,

null == undefined // true
null === undefined // false

They represent slightly different things:

  • undefined represents the absence of a value, such as before an identifier/Object property has been created or in the period between identifier/Function parameter creation and it's first set, if any
  • null represents the intentional absence of a value for an identifier or property which has already been created

  • They are different types of syntax:

  • undefined is a property of the global Object, usually immutable in the global scope. This means anywhere you can define an identifier other than in the global namespace could hide undefined from that scope (although
    things can still be undefined)
  • null is a word literal, so it's meaning can never be changed and attempting to do so will throw an Error.

  • The similarities between null and undefined

    null and undefined are both falsy

    if (null) console.log("won't be logged");
    if (undefined) console.log("won't be logged");

    Neither null or undefined equal false

    false == undefined // false
    false == null // false
    false === undefined // false
    false === null // false

    Using undefined

  • If the current scope can't be trusted, use something which evaluates to undefined, for example void 0;.
  • If undefined is shadowed by another value, it's just as bad as shadowing Array or Number.
  • Avoid setting something as undefined. If you want to remove a property bar from an Object foo, delete; instead.
  • Existence testing identifier foo against undefined could throw a Reference Error, use typeof foo against "undefined" instead.

  • Conclusion

    In this page (written and validated by ) you learned about Javascript Null and Undefined . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript Logic Operators.

    Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

    Share On:

    Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.