Bheesham Persaud

Back to the blog index.

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):

1
2
3
4
5
6
7
> var undefined = "FOO"; undefined;
undefined
> (function() {
...   var undefined = "BAR";
...   return undefined;
... }());
'BAR'

The results are different in Guile:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
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:

1
2
3
4
5
6
7
8
9
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:

1
2
3
4
5
((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:

1
2
3
4
jerry> var undefined = "FOO"; undefined;
undefined
jerry> (function() { var undefined = "BAR"; return undefined; }());
BAR

And Jsish:

1
2
3
4
5
6
# 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:

1
2
3
> 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