3

When we are requiring node modules such as express or bodyParser we will use the var keyword to create a variable and assign the module. Can't we use const to declare such modules? That is, instead of this

var express = require('express');
var app = express();

should we do this

const express = require('express');
const app = express();

What is the best way? And why is that?

davejagoda
  • 2,420
  • 1
  • 20
  • 27
Dhanuka Perera
  • 1,395
  • 3
  • 19
  • 29
  • 4
    `const` is preferred in ES2015 code because a) it has block scope and proper hoisting behavior b) you can't modify it so yes. I would use `const`. – Andrew Li May 27 '17 at 04:24
  • 7
    The new "golden rule" is: Use `const` until you can't – Sterling Archer May 27 '17 at 04:27
  • Possible duplicate of [const vs let when calling require](https://stackoverflow.com/questions/28135485/const-vs-let-when-calling-require) – str May 27 '17 at 08:46

1 Answers1

4

Mutability should be opt in, rather than opt out.

Whenever possible make everything const.

arboreal84
  • 2,086
  • 18
  • 21