简体   繁体   中英

Are commas necessary in Node.js?

Are there risks incurred when omitting commas in variable declarations for node.js? For example, declaring some global variables like the following works just fine:

express = require('express')
jade = require('jade')

And I don't want to write commas if it's safe not to write them (I don't care about "beauty/clarity of code" arguments).

Important : I mean commas, not semicolons (got 3 answers about semicolons). It's perfectly OK and even recommended to remove semicolons from node.js. The creator of npm also does it: http://blog.izs.me/post/3393190720/how-this-works

If in doubt, check the latest javascript specs: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

Note that you also don't need to write

var

for global variables.

But this question is about "commas" so please don't replace commas by semicolons by mistake when editing my question (done before).

In JavaScript, if you don't write semicolons ; they will be inserted for you, invisibly. And you may not always like where they go.

You are not technically required to end every statement with a semicolon. However, most consider it a good idea.

For more info, peruse through the results of this google search . We've been arguing about this topic for a long time.


Here's an example of why this is more complex than it appears at first glance. While you are not technically required to end every statement with a semicolon, there are a few cases where you MUST, or things break. You cannot completely omit them in most codebases.

foo.def = bar
(function() {
  // some self executing closure
})()

Looks simple enough right? Well the interpreter looks at that and does this:

foo.def = bar(function() {
  // some self executing closure
})()

Which is probably not what you were expecting. The way to fix it, is with a semicolon.

foo.def = bar;
(function() {
  // some self executing closure
})()

There a lot of cases like this. You can either learn them all, and only use them in those cases, and when you inevitably forget you try to debug your code that's doing something so strange and bizarre that you tear your hair out hours... "what do you mean wtfvar is not a function?!? It's not supposed to be a function!"

Or you can just use semicolons with consistency.

In short, no. The problems you are likely to run into will arise when you go to do things like code minifying and the compiler thinks that your two statements are one. Anyhow, if you choose not to use commas/semicolons, which is absolutely not recommended, you should be fine.

Node.js uses the V8 engine to read your code so it will pretty much behave like in Google Chrome. That said, not using semicolons is generaly a bad practice. The interpreter will try to understand your code and may be sometime mistaken (by your fault).

Check this out for a full explanation: Do you recommend using semicolons after every statement in JavaScript?

This very late answer just goes to clear confustion for others that might read this.

Since you're actually talking about commas , not semi-colons, I can only assume that you have a misunderstanding of what is implicitly being added by the engine.

Commas are not optional. And this code:

express = require('express')
jade = require('jade')

is being implicitly converted into this:

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

not this, which you might be expecting:

var express = require('express'),
    jade = require('jade');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM