3

I'm reading Javascript Allongé, and in it I see this code which is supposed to return false:

(() => 0) === (() => 0)

When I run this on the commandline (Ubuntu 14.04) using Nodejs, I get three dots: ..., after which I cancel using CTRL-C.

I start node.js with the following command: nodejs, not node. Using --harmony doesn't make a difference.

Node.js version: v0.10.25

Why don't I get a result back? I thought about using Nodejs as commandline test utility, but maybe this is not a good idea?

SPRBRN
  • 2,154
  • 4
  • 30
  • 48
  • 3
    Are you running it with the `--harmony` flag? – Alex Jun 18 '15 at 15:33
  • 2
    You are attempting to use the new 6th edition syntax without using the `--harmony` flag to let Node you are doing so. – Jason Cust Jun 18 '15 at 15:34
  • 1
    What version of Nodejs are you using? –  Jun 18 '15 at 15:34
  • possible duplicate of [node example.js and three dots, what's next?](http://stackoverflow.com/questions/20756568/node-example-js-and-three-dots-whats-next) – Alex Jun 18 '15 at 15:37
  • I'm on Ubuntu, and the command `node` doesn't work. I have to use `nodejs`, and using `nodejs --harmony` doesn't make a difference. Both result in the three dots. On my Mac however, it does work as described here. – SPRBRN Jun 18 '15 at 20:38
  • @RalphWiggum - v0.10.25 is installed on Ubuntu. This is the stable version via apt-get. – SPRBRN Jun 18 '15 at 20:45
  • @Alex - this is not a duplicate. – SPRBRN Jun 18 '15 at 20:56
  • 1
    @SPRBRN In that case, `--harmony` is not supported on that version. [Uninstall your current version of Nodejs using your package manager then follow these instructions](https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager#debian-and-ubuntu-based-linux-distributions). Once that is installed, then use the `--harmony` flag and it should work fine. –  Jun 18 '15 at 21:44
  • @SPRBRN It sort of is. You asked why node returned `...`, and that question regards the same thing. (Invalid javascript) – Alex Jun 19 '15 at 09:28

2 Answers2

6

The simple answer as to why is returns false is because even though the functions do the same thing and look the same, they are initialized in two different memory locations (for objects like functions, plain objects, and arrays, the strict equality operator === checks the memory location of that object.)

Also, if you're going to use Node.js, you have to make sure it is interpreting it as ECMAScript6 (otherwise, () => 0 is not valid ES5, which is default for Node.js).

You can use the --harmony flag:

node --harmony app.js

See this question about "What does node --harmony do?" for more info about using ES6 in Node.js. A brief quote from the top answer:

it seems harmony enables new ECMA features in the language. The reason your file won't run without harmony is because app.js is probably using non-backward compatible features from the new ECMA standard (like block scoping, proxies, sets, maps, etc.)

To explain why you might be seeing the three dots, see this question:

So basically, you opened node in an interactive terminal, and then typed node example.js, so it is trying to run that as if it were JavaScript. It shows the three dots because that is not valid JavaScript code, and it is waiting for you to type more code that might make it valid.

output

The above is my output. This is what I ran in my terminal (using 0.12.21):

> $ node --harmony
> (() => 0) === (() => 0)
> false
Community
  • 1
  • 1
Josh Beam
  • 17,535
  • 2
  • 32
  • 63
  • @Shashank, my answer says which version of Node I'm running: `0.12.21`. – Josh Beam Jun 18 '15 at 15:49
  • 1
    Thank you, I updated to node v0.12.4 and it worked :) +1 – Shashank Jun 18 '15 at 15:56
  • I'm on Ubuntu, and the command `node` doesn't work. I have to use `nodejs`, and using `nodejs --harmony` doesn't make a difference. Both result in the three dots. On my Mac however, it does work as described here. – SPRBRN Jun 18 '15 at 20:37
0

Remove nodejs:

sudo apt-get remove nodejs

Install NVM (Node Version Manager). Check the Github page for the latest version and copy the correct command there. This is for NVM version 0.25.4:

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.25.4/install.sh | bash

Logout, login, and nvm should work. Then install the node.js version you need, optionally multiple versions next to eachother:

nvm install 0.12.5

When this has finished, you can start node with the harmony option:

node --harmony

Now the following code should return false:

(() => 0) === (() => 0)
SPRBRN
  • 2,154
  • 4
  • 30
  • 48