Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the value of typeof null change inside a loop?

Tags:

javascript

v8

Executing this snippet in the Chrome console:

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

should print 1000 times false, but on some machines will print false for a number of iterations, then true for the rest.

enter image description here

Why is this happening? Is it just a bug?

like image 367
Agos Avatar asked Jun 21 '16 08:06

Agos


People also ask

Why does typeof null return object?

The reasoning behind this is that null , in contrast with undefined , was (and still is) often used where objects appear. In other words, null is often used to signify an empty reference to an object. When Brendan Eich created JavaScript, he followed the same paradigm, and it made sense (arguably) to return "object" .

What is the output of typeof null?

In JavaScript null is "nothing". It is supposed to be something that doesn't exist. Unfortunately, in JavaScript, the data type of null is an object. You can consider it a bug in JavaScript that typeof null is an object.

When using the JavaScript typeof () operator What does a value of null mean?

The value null represents the intentional absence of any object value. It is one of JavaScript's primitive values and is treated as falsy for boolean operations.

Why is null an object in JavaScript?

The JavaScript specification says about null : null is a primitive value that represents the intentional absence of any object value. If you see null (either assigned to a variable or returned by a function), then at that place should have been an object, but for some reason, an object wasn't created.


1 Answers

There is a chromium bug open for this:

Issue 604033 - JIT compiler not preserving method behavior

So yes It's just a bug!

like image 94
Slumber86 Avatar answered Sep 20 '22 13:09

Slumber86