JaneJ
JaneJ

Reputation: 76

jshint gives "missing semicolon" or not based on whether the function is void?

I am using jshint in VSCode. jshint gives a "missing semicolon" warning at the end '}' of the function below

void function doSomething(){
    console.log('Hello, World');
}

enter image description here

And adding the semicolon satisfies jshint:

enter image description here

But after I remove void, the warning goes away without the semicolon:

void function doSomething(){
    console.log('Hello, World');
}

enter image description here

What is the logic behind this? More generally, is there official style guidance such as PEP8 in Python for Javascript addressing the best practice for semicolon?

Upvotes: 1

Views: 451

Answers (1)

Quentin
Quentin

Reputation: 943561

With the linting rules you have:

  • Function declarations do not need to be followed by a semi-colon.
  • Expressions, including those which include a function expression, do.

By putting the void operator before the function keyword, you force it into expression context.

Since it is in expression context, you do nothing with the function in the expression, and you void the result, it becomes pointless. The expression doesn't do anything at all. It doesn't even create a variable with the function stored in it, which is why this errors:

void function x() { console.log(1) };
x();

More generally, what's the best practice rule on semicolns in javascript?

A highly opinionated subject.

Upvotes: 2

Related Questions