3
function foo() { return typeof null === 'undefined'; }
for (var i=0; i<1000; i++) console.log(foo());

Run code in Chrome console and see results:

132 false
868 true

But why?

Denisio
  • 57
  • 2
  • 5
  • What are the numbers for? In my mind, null should never equal undefined, as null is a value in itself, but undefined is just showing a variable as unassigned – TechnicalTophat Jun 20 '16 at 19:58
  • `(132)` and `(868)` (when inside small "bubbles") refer to the number of times each line has been repeated. In your case, `false` has been printed the first 132 cases, and `true` in the last 868 cases. – Frxstrem Jun 20 '16 at 20:00
  • Honestly, this seems like a bug in Chrome's Javascript engine. `typeof null === "object"` is [the expected behavior](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof#null), and `typeof null === "undefined"` should never be `true`. – Frxstrem Jun 20 '16 at 20:03
  • This is really interesting question. I do not know why but I would expect the output to be 1000's of true or false but not both. I'm curious whether Google Chrome developers would contribute in your thread. –  Jun 20 '16 at 20:20
  • 2
    This is a bug in the Chrome V8 JIT compiler, and only exists in Chrome 50: https://bugs.chromium.org/p/chromium/issues/detail?id=604033 – Kyle Falconer Jun 20 '16 at 20:26
  • @KyleFalconer: Not sure whether you should post that as an answer. – Bergi Jun 20 '16 at 20:53
  • @KyleFalconer not only V50, I'm test at chrome 51.0.2704.103m (64 bit) and code worked exactly as shown. – Denisio Jun 21 '16 at 05:01
  • Firefox 44.0.2: [screenshot](https://habrastorage.org/files/a49/abc/0dd/a49abc0dd92d4bc49a7dd5f66aab55e4.png) – Denisio Jun 21 '16 at 05:10

0 Answers0