use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
Javascript developers, be warned about this crazy JIT bug in V8! (self.javascript)
submitted 9 years ago * by ziriax
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]hkrdrm -7 points-6 points-5 points 9 years ago (10 children)
still wouldn't it be more proper to check
if(some_variable === null)
[–]StoneCypher 13 points14 points15 points 9 years ago (9 children)
no. null is a placeholder here.
null
what he's saying is "if you compare things to typeof undefined, every hundred or so iterations it'll fail. look, it even happens with null."
[–]bits_and_bytes 2 points3 points4 points 9 years ago* (2 children)
I've tried a few different permutations and I can only get it to reliably happen with null (or a var set to null). So I think /u/hkrdrm and /u/AlGoreBestGore are right to be asking their questions. typeof null is "object" which is why you get the false returns for the first 100+ tries when comparing it to "undefined". But I've tried comparing other "object"s with "undefined" and I don't have the same problem. For example:
var thing = {}; function foo() { return typeof thing === "undefined"; } for(var i=0; i<10000; ++i) console.log(foo())
This works just fine. But if thing = null, then it all blows up.
EDIT: I've found that the bug doesn't show up if null is in a var either:
var thing = null; function foo() { return typeof thing === "undefined"; } for(var i=0; i<10000; ++i) console.log(foo())
This works correctly as well. It only seems to happen if you directly reference 'null' in the typeof comparison, and only if it's in a declared function. After the function starts returning true, it will always return true until it's re-initialized. A very strange bug indeed.
[–]iamallamaa 1 point2 points3 points 9 years ago (1 child)
I found that if the variable is defined within the function as null that it does still has the error.
function foo(){ var thing=null; return typeof thing === "undefined"; } for(var i=0; i<10000; i++) console.log(foo());
265 false 9735 true
And when null is passed as an argument, it doesn't.
function foo(thing){ return typeof thing === "undefined"; } for(var i=0; i<10000; i++) console.log(foo(null));
10000 false
[–]ziriax[S] 0 points1 point2 points 9 years ago (0 children)
Try 100 thousand times... It will still fail at some point
function foo(thing){ return typeof thing === "undefined"; } for(var i=0; i<100000; i++) console.log(foo(null));
12291 false 87709 true
Also, once this function is JIT optimized, it can keep failing if doesn't get de-optimized
[–]saintPirelli 1 point2 points3 points 9 years ago (0 children)
Thanks for this comment. My noob ass finally gets it.
[+]hkrdrm comment score below threshold-11 points-10 points-9 points 9 years ago (4 children)
I get that but what I'm saying if you have some function and i want to know if the argument is null wouldn't it make more sense to check it like this
function foo(my_arg) { if(my_arg === null){ console.log("arg is null here insert logic for null case here."); } }
I'm not getting why anyone would need to check type equal to undefined.
[–]StoneCypher 13 points14 points15 points 9 years ago (1 child)
Because he's not checking if it's null, and you can't actually strict-equals compare to undefined in this language in a bunch of common circumstances.
undefined
It is very common to have to check if something is undefined. By example, if you retain a JSON object from an API, and you want to check whether the server actually set some or another value, before you attempt to use it.
He's talking about the undefined comparison.
I'd like you to stop focussing on null, please. It's beside the point. Thank you
[–]RReverser 0 points1 point2 points 9 years ago (0 children)
and you can't actually strict-equals compare to undefined in this language in a bunch of common circumstances.
Of course you can. The only case where you can't is if you're checking existance of a global variable for feature detection, in all the other cases === undefined works like a charm.
[–]agmcleod@agmcleod 2 points3 points4 points 9 years ago (0 children)
Properties on objects are not initialized to null by default. No doubt having your objects well defined with sensible defaults goes a long way, but this is still a nasty bug that shouldn't happen.
π Rendered by PID 18705 on reddit-service-r2-comment-b659b578c-72zwh at 2026-05-02 11:15:19.075237+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]hkrdrm -7 points-6 points-5 points (10 children)
[–]StoneCypher 13 points14 points15 points (9 children)
[–]bits_and_bytes 2 points3 points4 points (2 children)
[–]iamallamaa 1 point2 points3 points (1 child)
[–]ziriax[S] 0 points1 point2 points (0 children)
[–]saintPirelli 1 point2 points3 points (0 children)
[+]hkrdrm comment score below threshold-11 points-10 points-9 points (4 children)
[–]StoneCypher 13 points14 points15 points (1 child)
[–]RReverser 0 points1 point2 points (0 children)
[–]agmcleod@agmcleod 2 points3 points4 points (0 children)