?
Solved

How is NULL an Object in Javascript?

Posted on 2016-11-01
4
Medium Priority
?
84 Views
Last Modified: 2016-11-01
I'm still popping the hood on JavaScript, so this particular concept is bizarre to me.

With JavaScript, you've got objects and then you have properties of those objects:

var myCar = new Object();
myCar.make = "Ford";
myCar.model = "Mustang";
myCar.year = 1969;

I get that.

But recently, I've had to wrap my brain around how, in JavaScript, NULL is considered an object.

How does that work and why does that make a difference?
0
Comment
Question by:brucegust
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Assisted Solution

by:James Bilous
James Bilous earned 800 total points
ID: 41868777
In JS null is not an object it is a primitive type. By assigning null to something you are saying it exists but it has no known value. Undefined, on the other hand, means that no notion of that thing exists at all. The fact that typeof(null) reveals 'Object' is an error in the implementation of JS that has not been fixed in order to prevent breaking legacy code.
0
 
LVL 58

Accepted Solution

by:
Julian Hansen earned 1200 total points
ID: 41868793
In JS null is not an object it is a primitive type.
console.log(typeof null);

Open in new window

Having said that the reason goes back to the first version of JS - technically it is a bug but has remained to protect existing code.

In the first implementation of JavaScript, JavaScript values were represented as a type tag and a value. The type tag for objects was 0. null was represented as the NULL pointer (0x00 in most platforms). Consequently, null had 0 as type tag, hence the bogus typeof return value
source - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#null

In JavaScript, typeof null is 'object', which incorrectly suggests that null is an object (it isn’t, it’s a primitive value, consult my blog post on categorizing values for details). This is a bug and one that unfortunately can’t be fixed, because it would break existing code. Let’s explore the history of this bug.

The “typeof null” bug is a remnant from the first version of JavaScript. In this version, values were stored in 32 bit units, which consisted of a small type tag (1–3 bits) and the actual data of the value. The type tags were stored in the lower bits of the units. There were five of them:
source - http://www.2ality.com/2013/10/typeof-null.html
2
 

Author Closing Comment

by:brucegust
ID: 41869011
Thank you, gentlemen!
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41869031
You are welcome.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question