Yet another javascript wat moment

๐ŸŽ™๏ธ BonaparteCockletit ยท 431 points ยท Posted at 14:06:10 on June 21, 2016 ยท (Permalink)


Saved comment

qqqyqnz ยท 40 points ยท Posted at 20:23:11 on June 21, 2016 ยท (Permalink)

It's not a wat moment, it's a bug. Actual wat moments are language design choices.

BlueShellOP ยท 47 points ยท Posted at 06:20:57 on June 22, 2016 ยท (Permalink)

The real joke is that we're not even sure if it's a bug or a feature of Javascript anymore.

[deleted] ยท 50 points ยท Posted at 14:12:47 on June 21, 2016 ยท (Permalink)*

[deleted]

Laat ยท 96 points ยท Posted at 14:33:34 on June 21, 2016 ยท (Permalink)*

it is a JIT bug in chrome 51

relevant thread over at /r/javascript.

[deleted] ยท 24 points ยท Posted at 16:57:42 on June 21, 2016 ยท (Permalink)
DrScabhands ยท 1 points ยท Posted at 13:15:27 on June 22, 2016 ยท (Permalink)

I thought you were going to link to this

LoveandRockets ยท -1 points ยท Posted at 20:19:28 on June 21, 2016 ยท (Permalink)
backflipbail ยท 1 points ยท Posted at 05:45:45 on June 22, 2016 ยท (Permalink)

Jitterbug

spambot5546 ยท 4 points ยท Posted at 14:52:47 on June 21, 2016 ยท (Permalink)

The js typeof operator identifies the datatype of a given value, either a literal or the value in a variable. I can do

var i = "a";

console.log(typeof i);

and it'll print "string" because that's a string.

If an variable has not been assigned a memory location it is "null" and, especially in js, cannot be meaningfully said to have a type.

I tried running this in jsfiddle and it considered null to be an object. Using an undefined variable e.g.

var i;

console.log(typeof i);

gave me "undefined", but did so consistently.

pxndxx ยท 13 points ยท Posted at 18:19:57 on June 21, 2016 ยท (Permalink)

But null is an object in JS. in your second example, i is a variable of type undefined, and its value is undefined too.

Variables that haven't been assigned a value in JS are undefined, not null.

OptimisticLockExcept ยท 1 points ยท Posted at 17:14:15 on June 24, 2016 ยท (Permalink)

Actually null is not an object. The ECMAScript specification defines the types in ECMAScript. There is a Null Type and an Object type. (and other types)

The specification also defines that typeof null === "object"

Whether or not this was a bug originaly or not is arguable.

I'd link the spec but i'm on mobile

AquaWolfGuy ยท 5 points ยท Posted at 20:41:51 on June 21, 2016 ยท (Permalink)

Variables without a memory location aren't anything since they don't exist. Variables that have been declared and allocated but not assigned to (as in your second example) have the value undefined. typeof undefined is "undefined". null is not the same as undefined.

Browsing_From_Work ยท 1 points ยท Posted at 15:43:46 on June 22, 2016 ยท (Permalink)

Regardless if null is an object or not, the result of typeof null shouldn't change during execution.

Matizaurus ยท -6 points ยท Posted at 14:32:41 on June 21, 2016 ยท (Permalink)

Doesn't happen on Firefox. User agent stuff?

jonatcer ยท 32 points ยท Posted at 15:17:07 on June 21, 2016 ยท (Permalink)

Because it's not a Javascript issue.

more_downvotes_pls ยท 8 points ยท Posted at 14:38:18 on June 21, 2016 ยท (Permalink)

All return false for me in Chrome 51 on OS X.

Is it always 4917/5083 or is it random? Curious if you get the same with ===

svtguy88 ยท 8 points ยท Posted at 15:53:06 on June 21, 2016 ยท (Permalink)

Appears to be somewhat random, but only from machine to machine. I get 4099 false and 5901 true for both == and ===. I get the same result with multiple runs.

Chrome 51.0.2704.103 on Windows 10. I'm curious what the Linux build does. I'm guessing it mimics OS X's results, but will have to wait till I get home to check (no *nix on the work laptop).

[deleted] ยท 25 points ยท Posted at 17:52:12 on June 21, 2016 ยท (Permalink)

Appears to be somewhat random

someone make a random number generator based on this

Cley_Faye ยท 2 points ยท Posted at 18:37:39 on June 27, 2016 ยท (Permalink)

Last time we used undefined behavior to generate randomness we had to wipe all our ssh keys, so no thanks :|

taar779 ยท 9 points ยท Posted at 16:35:50 on June 21, 2016 ยท (Permalink)

Linux Chrome Version 51.0.2704.103 (64-bit)

for (let i=0; i<10000; i++) { console.log(typeof null == 'undefined'); }
4917 false
5083 true

Same result each time I ran it.

Chromium Version 51.0.2704.84 (64-bit) has the same result as well.

Saveman71 ยท 3 points ยท Posted at 16:49:22 on June 21, 2016 ยท (Permalink)

On Linux, build 51.0.2704.84 (64-bit), same results as well

nice_comment_thanks ยท 2 points ยท Posted at 18:19:06 on June 21, 2016 ยท (Permalink)

I'm on OS X, chrome version 51.0.2704.103 (64-bit), also 4917 false / 5083 true

more_downvotes_pls ยท 2 points ยท Posted at 16:45:54 on June 21, 2016 ยท (Permalink)

Weird. My OS X version is 51.0.2704.103 (64-bit) for what it's worth.

for (let i=0; i<10000; i++) { console.log(typeof null == 'undefined'); }
10000 false
nice_comment_thanks ยท 2 points ยท Posted at 18:20:57 on June 21, 2016 ยท (Permalink)

That's weird! I'm also on OS X (10.11.5), also with Chrome version 51.0.2704.103 (64-bit), but I get 4917 times false and 5083 times true.

more_downvotes_pls ยท 2 points ยท Posted at 19:15:11 on June 21, 2016 ยท (Permalink)

Ah, I got your numbers when I made a new file and just opened it in the browser.

Before I was I had just plugged it into my current project environment which is running through Babel and all sorts of ES6 and React things. I believe Babel is probably compiling scoped var equivalents of let or something so that that explains it.

wpreggae ยท 2 points ยท Posted at 16:06:19 on June 21, 2016 ยท (Permalink)

I get the same false/true count

[deleted] ยท 1 points ยท Posted at 22:09:20 on June 21, 2016 ยท (Permalink)

I'm on Chrome 51.0.2704.103 on Windows 10 too and get the exact same results as you: 4099/5901.

xXAndrew28Xx ยท 1 points ยท Posted at 23:50:53 on June 21, 2016 ยท (Permalink)

I got the exact same result as you. Outdated Chrome (51.0.2704.84 32 bit) on Windows 7 64 bit.

Fs0i ยท 5 points ยท Posted at 17:02:29 on June 21, 2016 ยท (Permalink)

It's a Chrome 51 Windows / Linux JIT-Bug. It's "random" - as soon as V8 JITs the method it replaces null by undefined internally for speed (since they should behave the same), which throws typeof off.

anotherdonald ยท 1 points ยท Posted at 17:57:46 on June 21, 2016 ยท (Permalink)
> var x = undefined
undefined
> x === undefined
true
> x === null
false
> var y = null
undefined
> y === undefined
false
> y === null
true
Fs0i ยท 5 points ยท Posted at 18:55:06 on June 21, 2016 ยท (Permalink)

Sorry if that was a bit unclear: V8 thinks that null / undefined are exchangeable in the function to optimise, when they obviously are not.

Cilph ยท 37 points ยท Posted at 14:21:10 on June 21, 2016 ยท (Permalink)

Am I going to have to start doing this?

var x = 2;
x = 2; // to make sure
doStuff(x);
x = 2; // to make sure x doesn't lose its value
jonatcer ยท 33 points ยท Posted at 15:18:58 on June 21, 2016 ยท (Permalink)

No, because it's a bug in Chrome.

jerkenstine ยท 2 points ยท Posted at 23:53:31 on June 21, 2016 ยท (Permalink)
boxingdog ยท 1 points ยท Posted at 01:33:28 on June 22, 2016 ยท (Permalink)

don't want to scare you but js does hoisting

Cilph ยท 1 points ยท Posted at 05:31:25 on June 22, 2016 ยท (Permalink)

Yup...

hanszimmermanx ยท 7 points ยท Posted at 20:52:57 on June 21, 2016 ยท (Permalink)

If anything its a chrome/v8 wat moment but I guess it doesn't matter, the JS hate sheep upvotes anyway.

Tinfoilpain ยท 3 points ยท Posted at 00:03:13 on June 22, 2016 ยท (Permalink)

it's b'aaaaaad

jerkenstine ยท 1 points ยท Posted at 23:54:01 on June 21, 2016 ยท (Permalink)

It's already fixed anyways.

jerkenstine ยท 3 points ยท Posted at 23:55:30 on June 21, 2016 ยท (Permalink)
coladict ยท 2 points ยท Posted at 10:47:42 on June 22, 2016 ยท (Permalink)

I've never actually used let. Is there any advantage over var? And most importantly, does it work on IE9+?

lukeilsluke ยท 2 points ยท Posted at 17:21:06 on June 23, 2016 ยท (Permalink)

A let stays in the scope of a code block and they're only hoisted to the top of the code block they're declare in. Not a ton of practical use, but there are some situations where you'd rather have them

DAVIDSPZGZ ยท 1 points ยท Posted at 08:55:30 on June 23, 2016 ยท (Permalink)

JavaScript is a full mystery

Miles-za ยท 1 points ยท Posted at 00:44:17 on June 24, 2016 ยท (Permalink)

I'd like to see someone make an RNG out of this.

TheDarkIn1978 ยท 0 points ยท Posted at 21:46:27 on June 21, 2016 ยท (Permalink)*

I actually just read something about this yesterday. Apparently, JavaScript has a long standing bug which outputs "object" instead of "null":

var a = null;    
typeof(a); //"object"

as one of JavaScript's 7 value types:

string, number, boolean, null, undefined, object and symbol

Apparently it has never been fixed, nor will ever be fixed, because of the vast amount of code already on the web that relies on this bug to function properly.

[EDIT]: SOURCE.

erikdesjardins ยท 4 points ยท Posted at 23:35:54 on June 21, 2016 ยท (Permalink)

Did they have a source for that claim?

In all of the ECMAScript specifications back to ES1 in 1997 (ctrl-f "the typeof operator"), it's specified that typeof null is 'object'.

I can't find the original 1995 LiveScript spec, but I don't think it would have been any different.

TheDarkIn1978 ยท -1 points ยท Posted at 00:34:06 on June 22, 2016 ยท (Permalink)*

[EDIT] Read for yourself here.

Ok, so I assume the bug has always been there ever since the first ES1 spec. Why would null return "object" instead of "null"?

var str = "";
console.log(typeof(str));  //"string"

var num = 0;
console.log(typeof(num));  //"number"

var bool = false;
console.log(typeof(bool));  //"boolean"

var n = null;
console.log(typeof(n));  //"object" (why not "null"?)

var un;
console.log(typeof(un));  //"undefined"

var obj = {};
console.log(typeof(obj));  //"object"

var sym = Symbol();
console.log(typeof(sym));  //"symbol"
compteNumero9 ยท 1 points ยท Posted at 08:30:23 on June 22, 2016 ยท (Permalink)

Hu ? This isn't a bug. typeof null is specified to be "object", just like typeof NaN is specified to be "number", which makes sense because null is the "missing" value when you expect an object.

poizan42 ยท 2 points ยท Posted at 16:26:30 on June 27, 2016 ยท (Permalink)

It was probably a bug (or at least "unintentional behavior") in Netscape Navigator 2.0 that later got enshrined into the standard to keep backwards compatibility.

compteNumero9 ยท 1 points ยท Posted at 16:31:51 on June 27, 2016 ยท (Permalink)

In fact you're right. I checked and even Eich thinks it's mostly a bug.

TheDarkIn1978 ยท 0 points ยท Posted at 10:50:34 on June 22, 2016 ยท (Permalink)

Ok, but unlike null, NaN isn't one of JavaScript's 7 value types, so there at least appears to be some inconsistency.

OKB-1 ยท 0 points ยท Posted at 16:18:11 on June 22, 2016 ยท (Permalink)

JavaScript dev protip: Try to avoid ever using typeof.