Ichooz Ichooz - 6 months ago 13
Javascript Question

Javascript comma syntax

Why do so many developers write commas this way?

var npm = module.exports = new EventEmitter
, config = require("./lib/config")
, set = require("./lib/utils/set");


Not this way?

var npm = module.exports = new EventEmitter,
config = require("./lib/config"),
set = require("./lib/utils/set");

Answer

They write them with the "," at the beginning of the line to make it easier to maintain the code (add lines or remove/comment out lines).

Given this:

var npm = module.exports = new EventEmitter
  , config = require("./lib/config")
  , set = require("./lib/utils/set");

It's much cleaner and easier to do this:

var npm = module.exports = new EventEmitter
//  , config = require("./lib/config")
  , set = require("./lib/utils/set");

as well as add new lines like this:

var npm = module.exports = new EventEmitter
  , config = require("./lib/config")
  , anothervalue = require("./lib/aval")
  , anothervalue2 = require("./lib/aval2")
  , set = require("./lib/utils/set");