2

I found out something weird about node.js today:

$ node
> console.log(_)
ReferenceError: _ is not defined
    at repl:1:13
    at REPLServer.defaultEval (repl.js:130:27)
    at bound (domain.js:257:14)
    at REPLServer.runBound [as eval] (domain.js:270:12)
    at REPLServer.<anonymous> (repl.js:277:12)
    at REPLServer.EventEmitter.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:202:10)
    at REPLServer.Interface._line (readline.js:531:8)
    at REPLServer.Interface._ttyWrite (readline.js:812:14)
    at ReadStream.onkeypress (readline.js:101:10)
> function foo() {}
undefined
> console.log(_)
undefined
undefined

The same thing happens after I create a function expression:

$ node
> console.log(_)
ReferenceError: _ is not defined
    at repl:1:13
    at REPLServer.defaultEval (repl.js:130:27)
    at bound (domain.js:257:14)
    at REPLServer.runBound [as eval] (domain.js:270:12)
    at REPLServer.<anonymous> (repl.js:277:12)
    at REPLServer.EventEmitter.emit (events.js:107:17)
    at REPLServer.Interface._onLine (readline.js:202:10)
    at REPLServer.Interface._line (readline.js:531:8)
    at REPLServer.Interface._ttyWrite (readline.js:812:14)
    at ReadStream.onkeypress (readline.js:101:10)
> (function () {}())
undefined
> console.log(_)
undefined
undefined

It's pretty cool, and handy for function arguments which you intentionally want to leave as undefined, but why does it happen? I am using node version v0.11.13 on Arch Linux.

4
  • <rant>The whole _ thing is staggeringly frustrating when you're trying to use code that expects _ to be the underscore library. The workarounds are hacky and don't really work; I recompiled to get rid of it--I don't use it anyway.</rant> Commented Sep 6, 2014 at 15:29
  • @DaveNewton but isn't repl suppose to work ONLY when you use node via command line? Not in a program itself? I'm confused because I came across this line "By executing node without any arguments from the command-line you will be dropped into the REPL" on the REPL docs. Commented Sep 6, 2014 at 15:39
  • +1 as well for bringing this feature into notice. I wouldn't have otherwise known or queried about this. Commented Sep 6, 2014 at 15:40
  • @I_Debug_Everything I'm not sure what you mean. If you type node you get the repl. But you can load arbitrary code into the repl, or cut-and-paste, etc. If your code includes the _ symbol things break rather badly. Commented Sep 6, 2014 at 17:36

1 Answer 1

2

It's a feature of the repl: _ returns the result of the last expression.

Of course, in some cases - like the console.log you have - an expression will not return a result. But yeah it's a handy way to get the value of the last expression.

This only happens in the REPL of course - if you type up the same node program and run it from a file, _ will be under your control.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.