Hacker News new | ask | show | jobs
by JonathanMerklin 1284 days ago
I just checked and this is true for the Node.js REPL (Tried in v12, v14, v16, and v18) as well, which I'm also just learning for the first time (and I had the same workflow as you before this). Neat!
1 comments

Ha! Funny that you'd even think to check. In Scala and Python (I'm guessing other langs as well), the underscore can be used for ignored variables, among other things. Never thought it had another usage in the REPL. Actually really useful!
Yeah, I've used it in Haskell for ignored variables, holes, type wildcards, etc.

Another interesting little rabbit hole that this little conversation led me to: In the Chrome (and Firefox) developer tools, the variable for the previous output is "$_", which I imagine that is the case because of how common it was to assign the main export of the Underscore.js library to "_" (and in the days before a lot of websites would mostly e.g. have their site's code in a webpack-induced closure, they would e.g. grab underscore (or lodash, in those times?) from a CDN and pollute the global scope).

Since _ is a valid identifier, it also turns out that in Node.js's REPL, it warns you when you clobber _, but (weirdly) not if the clobbering is with a block-scoped declaration.

  $ node
  Welcome to Node.js v18.12.1.
    Type ".help" for more information.
  > "asdf"
  'asdf'
  > _
  'asdf'
  > var _ = 1
  Expression assignment to _ now disabled.
  undefined
  > "asdf"
  'asdf'
  > _
  1

  $ node
  Welcome to Node.js v18.12.1.
  Type ".help" for more information.
  > const _  = 1
  undefined
  > _
  1
  > "asdf"
  'asdf'
  > _
  1
And obviously, there's no warning for assigning to it outside of the REPL (`node -e "var _ = 2;"`), which makes obvious sense to me.