Are commas needed in Node.js?

Are there any risks when omitting commas in variable declarations for node.js? For example, declaring any global variables, such as the following, works very well:

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

And I don’t want to write commas if I shouldn’t write them (I don’t need the arguments “beauty / clarity of code”).

It is important . I mean commas, not semicolons (I got 3 answers about semicolons). This is completely normal and it is even recommended to remove semicolons from node.js. The npm creator also does this: http://blog.izs.me/post/3393190720/how-this-works

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

Please note that you also do not need to write

 var 

for global variables.

But this question is about commas, so please do not replace commas with a semicolon by mistake when editing my question (made earlier).

+7
source share
4 answers

In JavaScript, if you do not write semicolons ; , they will be inserted seamlessly for you. And you may not always like where they go.

You do not need to technically complete each statement with a semicolon. However, most consider this a good idea.

See our Google search results for more information. We have been discussing this topic for a long time.


Here is an example of why it is more complicated than it seems at first glance. Although you are technically not required to end each statement with a semicolon, there are a few cases where you SHOULD or things break. You cannot completely omit them in most codebases.

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

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

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

Most likely, this is not what you expected. A way to fix this with a semicolon.

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

There are many such cases. You can study them all and use them only in those cases, and when you inevitably forget that you are trying to debug your code doing something so strange and strange that you will rip your hair for several hours ... "what you have in mind wtfvar not a function?!? It should not be a function! "

Or you can just use consistency semicolons.

+18
source

In short, no. The problems you are likely to encounter arise when you go to do things such as code reduction, and the compiler considers your two statements to be the same. Anyway, if you decide not to use commas / semicolons, which is absolutely not recommended, you should be fine.

0
source

Node.js uses the V8 engine to read your code, so it will behave like in Google Chrome. However, not using semicolons is common practice. The interpreter will try to understand your code and may be once erroneous (through your fault).

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

0
source

This very late answer simply goes to a clear disappointment for others who might read this.

Since you are really talking about commas , not half-columns, I can only assume that you have a misunderstanding of what is implicitly added by the engine.

Commas are optional. And this code:

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

implicitly converted to this:

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

not this what you expect:

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

All Articles