JS.BASE.NEW.PARENS

Enforce or disallow parentheses when invoking a constructor with no arguments

JavaScript allows the omission of parentheses when invoking a function via the new keyword and the constructor has no arguments. However, some coders believe that omitting the parentheses is inconsistent with the rest of the language and thus makes code less clear.

Copy
var person = new Person;

Rule Details

This rule can enforce or disallow parentheses when invoking a constructor with no arguments using the new keyword.

Options

This rule takes one option.

  • "always" enforces parenthesis after a new constructor with no arguments (default)
  • "never" enforces no parenthesis after a new constructor with no arguments

always

Examples of incorrect code for this rule with the "always" option:

Copy
/*eslint new-parens: "error"*/

var person = new Person;
var person = new (Person);

Examples of correct code for this rule with the "always" option:

Copy
/*eslint new-parens: "error"*/

var person = new Person();
var person = new (Person)();

never

Examples of incorrect code for this rule with the "never" option:

Copy
/*eslint new-parens: ["error", "never"]*/

var person = new Person();
var person = new (Person)();

Examples of correct code for this rule with the "never" option:

Copy
/*eslint new-parens: ["error", "never"]*/

var person = new Person;
var person = (new Person);
var person = new Person("Name");

The content on this page is adapted from the ESLint User Guide. Copyright © OpenJS Foundation and other contributors, www.openjsf.org. All rights reserved. https://eslint.org/docs/rules/