Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

14. Null > typeof Returns null Values as “object”

typeof Returns null Values as “object”

For a variable that has a value of null, the typeof operator returns 'object'. If you need to verify a null value, the ideal solution would be to see if the value you are after is equal to null. Below, we use the === operator to specifically verify that we are dealing with a null value.

Live Code

<!DOCTYPE html><html lang="en"><body><script>

var myObject = null;

console.log(typeof myObject); // logs 'object', not exactly helpful
console.log(myObject === null); // logs true, only for a real null value

</script></body></html>

Note

When verifying a null value, always use === because == does not distinguish between null and undefined.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint