Skip to main content
View on YouTube to comment and like!

Syntax Video

Why is typeof null "object"? | Netscape Source Code Dive

Chapter 1 00:00

Transcript

so I've been writing JavaScript for a very long time and I have always kind of just accepted the fact that type of null is object and a lot of people like to poke fun at JavaScript because of all these little quirks and once you've written enough JavaScript you get used to things like this and maybe you don't give it a second at thought but today I decided to figure out why why does this equal object is there a specific reason for this have they ever tried to fix it and that led me down a nice long Rabbit Hole so let's dive in so first I headed over to mdn to look up the type of operator and this actually has a table that describes all of the various types that you can pass into it and the corresponding strings that you're going to get back and lo and behold you see there if we pass in null it gives us back object and so you can see here it says in the first implementation of JavaScript JavaScript values were represented as a type tag and a value the type tag for objects was Zero null was represented as the null pointer which is also zero and consequently null had zero as a type tag hence the type of return value was object and this actually comes from a blog post written by Dr Axel rashme this post was from

2013 called the history of type of null and he dives into it he talks about why this is the case and talks about this type tag thing that was mentioned on mdn and then also shows the actual source code for the original implementation of the type of operator inside of JavaScript and so this is C code from the original source code for the original implementation of this and he actually found this by posting on Twitter so back when he was researching this blog post uh he actually posted about it and then evil pies responded with the source from the classic version of Mozilla which was Netscape and so I thought great we can dive in we can actually look at the original source code however it currently is throwing a not found error and so of course I went back I grabbed that original URL I plugged it into the Wayback machine it also returns a 404 not found I did some more digging and eventually I found the C file on the web archive but this is not the header file so the link originally was to a h file so if you're not familiar with C header files are kind of like descriptions of what's going to be implemented and then your C file is your actual implementation so we can see here is the original type of function right there but this has a lot

of references to things that are not defined in this file they're actually defined in the header file so I was like all right we need to find this header file and this led me down a pretty interesting history of Netscape and how it was originally open source so why are we even able to see this source code for Netscape well back in January of 1998 Netscape announced that by the end of the first quarter of 1998 they were going to release the source code for Netscape Communicator version 5.0 and so this is the actual source code that we're looking at now here we're potentially looking at a slightly later version of the code because we are looking at a snapshot that was taken on 2020 and this code has been iterated on since it was originally released uh so yeah it was open sourced back in 1998 and I wanted to find like the original original commit and I was able to do that so there's this repo here called the Mozilla CVS history if you're not familiar with CVS it is concurrent versions system so it's this is a Version Control System these days we pretty much use git for everything but CVS was an earlier version of Version Control that was used for the Netscape source code and I actually went back in

time and found the commit from 27 years ago that was called free the lizard and I was actually able to to find the source code and kind of dig into it and so if you head into the JS directory and then the source directory here is all of the source codes from 27 years ago and specifically the files that we're looking for are Js api. C and JS api.

and once you start digging into the code you can see here we actually have these type tags that they were referencing in that blog post so you have the type tag for objects ins double strings and booing and then each one of these has a specific value so objects are zero inss are one doubl are two strings are four and booleans are six and so essentially the way that JavaScript was implemented so once your code was compiled and running inside of the JavaScript engine every value was stored as a long so if you look at C data types long is a signed integer type with a minimum value of this and a maximum value of this and so essentially every running JavaScript program any values defined whether they're objects or strings or or numbers they're all actually at the end of the day getting stored inside of a long and to figure that out actually took a lot of digging so in C there 's a thing called a typee def which actually allows you to Define custom types but there's tons of indirection and so I kind of just have some notes Here of how I found it and so when you're looking in this header file you see this JS vow everywhere and so this is the type that's used to correspond to a value in

your JavaScript program but this isn't a native C type and so I dug through the source code and kind of like traced it backwards and so JS Val is actually defined from prw which is actually defined from PR wordt which comes from NPR Pub which is also in that source code repo and anybody that knows more about C or knows more about the history of this can uh can let me know in the comments what these specifically are but within that there's another header file that defines PR word and then finally if you trace that back PR word is defined as long so anywhere in the source code whenever you see JS file really we're looking at long values and you can see here that a long is 32 bits so that means when your program is running any variables that are of this type long or of type JS value are going to take up 32 bits so from there we can actually start to understand what these tags are used for and so I've drawn up a little table here where you can see the type on the left and then their tags on the right and so we're representing this as 32 bits and so when we say 32 bits you can just think of it as a series of 32 zeros or ones and so in the case of int we

have a tag of one and then 31 bits to the left that are used to store the actual value and so this is the one special case in all of our types whenever we're dealing with ins the tag length is only one bit but for all of our other types the tag length is three bits and so here we can see the object has a tag of zero and so when we come across a value with three zeros on the tag we know we're dealing with an object and then a double has a tag of Two And so when we come across a value that has a tag of 0 1 0 which is two and binary we know we're dealing with a double string has a value of four so when we come across a value with a tag of one 0 which is four in binary we know we're dealing with a string and then when we come across a value of six which is 110 in binary we know that we're dealing with a Boolean and so essentially the tag is those last three bits and then anything to the left is going to have the actual value of whatever we're trying to store here and so now that we have an idea of how the values are stored we can take a look at how null is stored and defined and so when we look at the codes you can see they actually have this macro defined here it's called JS Valore null this actually stores the literal value of null and you can see this then calls out to another macro

called object tojs value and passes in the value zero and then object to JS value just casts that value to a JS value so we already learned that a JS value is a long and the object that they passed in here was zero so literally the value stored for JS value null is aong with the value of zero and so now you can kind ofer to see what might happen here so because null is just zero it's going to have a tag of zero right because a 32-bit long with a value of zero is literally just going to have 32 zeros but if we have code that specifically is looking at that tag there it's going to think that this is an object and so now with with that knowledge we can kind of dig into the code and and specifically look at the type of value function and so this is a pretty straightforward function it takes in a jsv Val which of course is going to be a long and then runs several of these various macros to determine if it is that specific and so there's a macro here for JS value is void and in this case JS value void has a value inside of it that is this now we talk about JavaScript being convoluted look at all this indirection there's so many defines

that we have to go to but ultimately if you follow all of these macros and then resolve the actual value JS value void is equal to -2 to the 31st power minus one um basically we I did the maths here so this resolves to this which resolves to this this this and then you have some bitwise operators and eventually you get this number here which is actually the uh smallest possible 32bit value regardless that's what's inside of JS value void and so when this runs it basically compares the value if that value is equal to -2 to the 31st power minus one then the type is void and you can see here JS type void is just from this enum so this enum has all of the possible types undefined object function string number and booing and as you can see there is no type null inside of there now from there all of these other comparators are really looking at that tag value on the end of the value so if you look at JS value is object there's a macro here that's going to grab the tag which is those last few bits from the specific value to see if it is that specific tag so all of our tags are here and then that macro is going to pull the tag off of the number and so if you had

a real object where all of this was not zero it was like actual values then we'd pull off the tag see that the tag was zero and then know that this part here could be represented as an object and so that's what this JS Val tag macro is doing it's essentially doing all of the bit shifting to grab those last three bits so it can determine the tag of the specific number okay so basically if the tag is is zero then we know that it's an object but it's also possible that it's a function so we have some extra code here now JS Val is number checks to see if it's an integer or a double because if you remember from the tags we have double and we have int we don't actually have number and so all this does is gets the tag as if it were an integer gets the tag as if it were a double and if either of those is true then it is a number then for string all we have to do is pull off the tag compare it to our tag value and then same thing for Boolean so we've made it we've walked through the code and if you've been following you can see that there is literally no check deck for null here and so the code that I've been showing you I've actually extracted out so that I could get it to run now I'm not compiling the entire Netscape browser I just have some code here that does

specifically this type stuff and so in this example program I have a value here which is a JS Val and I'm storing JS value null inside of it and then we're calling our typo function which gives us back a JS type and enums inside of c are number based so each one of these is a number so 0 1 2 3 Etc and so from there we can pass that into another function called get type name which specifically takes that number and then Returns the index in this list of types and then here I'm just printing it out now the other thing I'm doing here is if that value is equal to null and so in C null is actually a macro that is void star of zero but if our specific value is equal to that we're also going to print out that it points to null and so if we compile and run this program you can see that the type of null gets detected as an object but it actually does point to null and if we want to try this out with other types we could try like a JS value for booing and so this is essentially a JS value that only has the tag for Boolean so this code doesn't make that much sense but ultimately because it has the tag of a Boolean whenever we run the code it's going to give us that type of

booing and it doesn't point to null and we could pass in any of these other types so JS value of like a double should give us back number it does and of course we could pass in the tag for string it gives us back string so ultimately there's just a bug in this code right literally all we need is probably one more if statement that checks to see is it null and then if that's the case then maybe type of value can actually return the string null so let me just update the code to do that now one of the most interesting things about this is they actually did have a macro Define called JS Val is null this exists in the code but they're just not using it but you can see it Compares that value with JS value null which actually has that null pointer inside of it but in this case we can actually use it now we will need to add another type so if we head back up to our enum we can add a type here called null and then in our array we just need to add null on the in here and that should do it now our type of function is going to check for null first and if it is null it's just going to return the type as null so let's actually pass in null here run the codes and look at that we fixed JavaScript and now it actually has

a type of that returns null so that's it right we just submit a PR to Mr JavaScript and everything's fixed well not exactly so there's actually a long history of uh people trying to fix this and there's a lot of discussion that happened around it and essentially this discussion started around ecmascript Harmony so back in 2008 it was announced that tc39 was actually going to start working on this thing called Harmony because back then there was a lot of ideas about how JavaScript could evolve and a lot of discussions around what should change or what shouldn't change and the idea was to kind of harmonize around all of these ideas and push forward on some common ground that's actually described in this note here from Brandon Ike back in 2008 and what's interesting about this is ecmascript 4 was never released so you've probably heard of ES3 and then you've probably heard of es5 and after that we got equiscript 6 or ES 2015 but there never was a version 4 and so there was a lot of discussion and and essentially we were talking about adding classes and and the module system and all this modern stuff that we have in JavaScript even back in 2008 uh but eventually that

was abandoned and then in 2009 they did released the fifth version of the ecmascript standard that added some newer stuff but it wasn't as big a leap as was described here and the reason I'm even bringing this up is because when they were having this discussion around new features and how they should make the language better the discussion of fixing the type of operator came up and they mentioned that in harmony there potentially could be an opt-in if you could think of it almost like strict mode where if you into this then type of null would return the string null and then if you weren't using the harmony version then it would return object as it was and there's a lot of discussion around this there's a lot of rationale this is actually super interesting to read Because you see all these people like Brandon Ike and Douglas crockford and Alan wor Brock all talking about well should we add this shouldn't we add this and ultimately they ended up not adding it so you can see here that this proposal was rejected and it was implemented in V8 but it turned out that it broke a lot of existing sites in the spirit of one JavaScript is not feasible and so you might not have even heard the term one JavaScript before and so this

led me down another rabbit hole but essentially all of the discussions around Harmony and like making the JavaScript language better culminated in es6 or ES 2015 and so this is the modern features of ecmascript of of JavaScript that we know and love one of the things you may not realize is their whole goal here was to maintain backwards compatibility and so there's actually another blog post from Axel back in 2014 called one JavaScript avoiding versioning an ecmascript and there was a long dra out process and discussion around well should ecmascript be versioned like should you be able to opt into a version of JavaScript where type of Works in a different way than it does in other versions of JavaScript and there's a lot of discussion around it but ultimately they decided against it and one of the examples that Axel has here in this blog post is in the python World there was a jump from python 2 to Python 3 and some of the changes in Python 3 were were backwards incompatible right there's some Python 3 programs that you cannot run through a python 2 interpreter and there was potential for JavaScript to go down that path but ultimately they decided against it and the term that they coined was one

JavaScript and one of their goals is essentially making ecmascript 6 completely backwards compatible and so this is one of the major reasons there are a lot of quirks in the language that still exist to this day essentially some very smart people went back and forth on this and and talked about well should we introduce breaking changes should we not and ultimately landed on this idea of backwards compatibility and ultimately I think this was a great decision so uh here Axel is saying programmers profit immediately from one JavaScript because it makes it easier to get started with ecmascript 6 imagine having a fractured web where like some websites are using the certain versions of JavaScript and some aren't I think it was a really good decision and this is why type of null returns object and probably will never return the string null and to close this off there was actually a post from Adam Argy yesterday that said uh Legos never change or update their shapes everything from day one still works with stuff in 2024 so nice sound familiar yeah the web is like that too and there's a lot of discussion in the comments but I think ultimately Adam is talking about like web standards like ecmascript and CSS

and HTML and this idea that you can take some of the code that was written 20 30 years ago and it will still run in the latest engines that have all these nice features but still are completely backwards compatible so that's the story of type of n hopefully you enjoyed this trip down memory lane and this dive into some Source codes and let me know what you think do you think we might get to a point where we'll have a breaking version of JavaScript or maybe type of will return something like no let me know down in the comments that's it for this one keep on coding I'll see you in the next one [Music]

Quote it with credit and a link. Quoting Syntax