Weird behaviour with different implementations of JS.

Weird behaviour with different implementations of JS.

I found a strange thing in JavaScript implementations. The following code returns the same results in both Firefox and Chromium (output from Node.js):

> var undefined = "FOO"; undefined;
undefined
> (function() {
...   var undefined = "BAR";
...   return undefined;
... }());
'BAR'

The results are different in Guile:

GNU Guile 2.0.14
Copyright (C) 1995-2016 Free Software Foundation, Inc.

Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
This program is free software, and you are welcome to redistribute it
under certain conditions; type `,show c' for details.

Enter `,help' for help.
ecmascript@(guile-user)> var undefined = "FOO"; undefined;
$1 = "FOO"
ecmascript@(guile-user)> (function() {
  var undefined = "BAR";
  return undefined;
}());
$2 = "BAR"
ecmascript@(guile-user)>

Hm. That's weird. Let's try Rhino:

Rhino 1.7.9 2018 03 15
js> var undefined = "FOO"; undefined;
js: uncaught JavaScript runtime exception: TypeError: TypeError: redeclaration of const undefined.

js> (function() {
  >  var undefined = "BAR";
  >  return undefined;
  > }());
BAR

Ah, similar to Gecko. I wonder how different some other implementations are.

Here's Duktape:

((o) Duktape [linenoise] 2.3.99 (v2.3.0-52-gb062b50a)
duk> var undefined = "FOO"; undefined;
= undefined
duk> (function() { var undefined = "BAR"; return undefined; }());
= "BAR"

And JerryScript:

jerry> var undefined = "FOO"; undefined;
undefined
jerry> (function() { var undefined = "BAR"; return undefined; }());
BAR

And Jsish:

# var undefined = "FOO"; undefined;
@:1.7: error: syntax error, unexpected UNDEF, expecting IDENTIFIER
ERROR
# (function() { var undefined = "BAR"; return undefined; }());
@:1.39: error: syntax error, unexpected UNDEF, expecting IDENTIFIER
ERROR

And MuJS:

> var undefined = "FOO"; undefined;
> (function() { var undefined = "BAR"; return undefined; }());
BAR

MuJS's REPL doesn't print "undefined".

Uh, my memory is bad and I'd rather not have to scroll up too often. Let's tabularize.

impl return val w/o lambda return val w/ lambda
Chromium (V8) undefined BAR
Firefox (Gecko) undefined BAR
Guile FOO BAR
Rhino throws exception BAR
Duktape undefined BAR
JerryScript undefined BAR
Jsish throws exception throws exception
MuJS undefined BAR