Comma and semicolon

In the last months I wrote a lot of code in node red for my home automation and even if I am a beginner, it's work fine.
I also copied a lot of code from this forum but sincerely I didn't understand the proper use of comma and semicolon into a function,
Probably sometimes are necessary, sometimes not but I'd like to know the specific use ....

example n 1: at the end of every line need semicolon or not?
var x = msg.payload;
flow.set("var_ora_onmill" ,x);
flow.set("var_lavatriceon" ,1);

exampe n 2: at the end of every line (msg) need comma or not? (or need semicolon?)
if (x == 0)
msg.labelcolorbloc = "white"
msg.bgcolorbloc = "red"
msg.labelbloc = "STOP"
return msg;

example n. 3: at the end of every line (msg) need semicolon?

if (x.includes("rain"))
msg.labelcolor ="#FB8C04"
msg.labelcolor ="white"
return msg;

The rule is to use a semi colon after every statement, but they can be omitted as JS will add them where required. ASI

I'm going to read, thank you ...

You will find that the standard style now more often omits ; rather than includes them. There are a couple of places you might need them but generally, they are no longer needed at all. So I disagree in general with the warning from that article (which is from 2018 and JS has move on a long way since then).

There is even a liniting standard called JavaScript Standard Style ( which is widely used by many organisations including Node.js itself.

All my code is written this way.

No, because each of those lines is a statement in its own right. If anything, you would use a ; (but really you don't need to).

You need comma's when you specify an OBJECT, not a statement:

const myObject = {
    'property1': 'I am the value of property1',
    'property2': 'I am the value of property2',

Note that trailing commas are permitted in JavaScript Objects and personally I recommend them because it makes it a lot easier if you need to re-order the lines or add a new property/value pair.

The other place you might use a comma in a statement is if you want to define multiple variables - personally I generally avoid this. Again because it is prone to mistakes and they can be hard to find because they are hard to read:

let x,y,z

No for the same reasons as the first response.

Another pet peeve of mine is the C-style brackets use (having the bracket on a separate line as you have done). I guess if you are used to C/C++ that is the common style but I dislike it because it pulls the eye away from the logic structure (in my view anyway). It also uses up extra lines on-screen which often means that you have to scroll more to see the code. This all wastes time and is a distraction.

1 Like

I'm the same - bit of a peeve for me too. I'd write it as...

if (x.includes("rain")) {
    msg.labelcolor = "#FB8C04";
} else {
    msg.labelcolor = "white";
return msg;
1 Like

I also use this format. I have also stopped using ; at the end of a line.

Detouring slightly, I now only use const & let. I haven't found a need to use var in any recent code

1 Like

OK, thank you all very much...

The "at all" is not needed and breaks the logic of the statement.

Just like life, there are always more than 1 camp. Adding them cause no issues (except for OCD suffers), omitting them can cause errors.

1 Like

Picky but true. :grinning:

Absolutely. All I can say is that StandardJS wouldn't have taken off as much as it has if it weren't a decent option.

When I'm writing JavaScript, I'm using VScode with ESLint set to StandardJS with a couple of tweaks. For example, I don't like double-quotes around strings - for 2 reasons: 1 is that single-quotes don't require a shift-key to type whereas double-quotes do (at least on a UK keyboard) and The extra pixels can be visually distracting (there's my OCD again :slight_smile: ).

Anyway, ESLint tells me if I need a ; somewhere - but it never has done, ever.

Was not trying to be picky, was more of a joke, playing on the not need semicolons. Sorry if it caused you offense.

Not taken as offensive I can assure you hence the grin emoji. A thick skin is required anyway to be a professional IT consultant and architect :rofl:

Emojis! I have difficulty seeing if they are smiling or sad.