Solved

How is NULL an Object in Javascript?

Posted on 2016-11-01
4
26 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
  • 2
4 Comments
 
LVL 7

Assisted Solution

by:James Bilous
James Bilous earned 200 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 51

Accepted Solution

by:
Julian Hansen earned 300 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 51

Expert Comment

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

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now