5

I discovered the following behavior in the NodeJS shell:

$ node
> function foo () { _ }
undefined
> _
undefined

Why is the _ variable defined? I expected to get ReferenceError: _ is not defined.

If I create an arrow function, the _ will be the function reference:

$ node
> () => _
[Function]
> _
[Function]
> _.toString()
'() => _'

Why is this happening?

After calling toString() on the _ variable, the _ is converted into string:

> _
'() => _'

I tried to use _ => () and we have the same issue:

$ node
> _ => {}
[Function]
> _
[Function]
> _.toString()
'_ => {}'
> _
'_ => {}'

I'm using node v5.6.0. This is not reproducible in a file but only in the NodeJS shell.

Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474
  • 2
    It's not an exact dupe but the answer is there. Basically, Node.js assigns the value of the last expression to `_`. [Official docs on the matter.](https://nodejs.org/api/repl.html#repl_repl_features) Try typing in this: `1 _ `. The value of `_` will be `1`. – Mike Cluck Mar 02 '16 at 20:15
  • @MikeC Post it as an answer, not in the comments. Or flag as duplicate. – Max Mar 02 '16 at 20:25
  • @MaxMastalerz I did flag it. Just threw in the comment for a brief explanation. – Mike Cluck Mar 02 '16 at 20:29
  • 1
    @MikeC Makes sense. Thanks! :-) So, it's a feature, not a bug. Learned something new. – Ionică Bizău Mar 03 '16 at 03:55

0 Answers0