Solved

How is NULL an Object in Javascript?

Posted on 2016-11-01
4
62 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 8

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 56

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 56

Expert Comment

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

Featured Post

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
jQuery Datatables.js Selecting a Row to get ID 5 36
Timer on div 5 23
javascript form submition 2 21
How do i use the await event in php echo 5 18
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

756 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