2

According to the answers to this question Const in JavaScript: when to use it and is it necessary? it would make sense to use const for gulpfile.js configuration, would it not?

But in every usage description of gulp-modules, I see var being used like for instance:

var gulp = require('gulp');
var argv = require('yargs').argv;

var autoprefixer = require('gulp-autoprefixer');

//...

As I understand it, these declarations never change while Gulp is running.

Is there a reason for using var? Is it something with node.js compiler? Or is this just a habit?

mayersdesign
  • 5,062
  • 4
  • 35
  • 47
  • `const` is quite new, so it may be that those are older examples. – Matthew Daly Sep 21 '18 at 10:04
  • @MatthewDaly so would it make sense for me to replace all var in the declaration of module references and paths with const for a performance boost? –  Sep 21 '18 at 10:09
  • Probably. If you're getting an object of some kind, such as Gulp, then I'd generally use `const` – Matthew Daly Sep 21 '18 at 11:06

2 Answers2

1

It's a necessary habit.
gulp is a module and can be used in multiple locations in a code file. But, if you overwrite it accidentally the whole code will crash.
It's better to depend on the computer or compiler. Instead of human accuracy.
If you use const , you can never overwrite the variable. That will avoid error.

Prasanta Bose
  • 674
  • 5
  • 13
1

I suppose you are not using babel with gulp. If you are using it with babel, it would be better to use const or let instead of var. If you are not using babel, then var is the only way for you. Read this blog post for further information: https://travismaynard.com/writing/using-babel-with-gulp

Or even better, this https://markgoodyear.com/2015/06/using-es6-with-gulp/

Lazar Nikolic
  • 4,261
  • 1
  • 22
  • 46