view raw
Hugo Hugo - 1 year ago 43
Node.js Question

const vs var while 'requiring' a module

I was reading the documentation for and I noticed that, in the examples, they use const to require a module and this made me wonder: is this a good practice? I mean, to me, this looked as a good idea.

A direct example from the link above is:

const BufferList = require('bl')

var bl = new BufferList()
bl.append(new Buffer('abcd'))
bl.append(new Buffer('efg'))

I also noticed the lack the semicolons in the example but well, that has been discussed elsewhere thoroughly.


The const makes perfect sense here:

  • It documents that the object reference is not going to change.
  • It has block scope (same as let) which also makes sense.

Other than that it comes down to personal preference (using var, let or const)