question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parenthesis around single arguments in arrow functions

See original GitHub issue

Always use parentheses around the arguments. Omitting the parentheses makes the functions less readable and only works for single arguments.

Why? These declarations read better with parentheses. They are also required when you have multiple parameters so this enforces consistency.

 // bad
 [1, 2, 3].map(x => x * x);

 // good
 [1, 2, 3].map((x) => x * x);

This stuck out to me as odd. It’s an intentional part of the spec to make it more readable for simple cases.

Anyways, I’m interested in discussing this.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

10reactions
jamiebuildscommented, Apr 3, 2015

Sure, the consistency is good. But I think the rule could likely be improved upon.

In my experience writing ES6 over the last year or so, 90% of the time when you are writing an arrow function expression it’s with only a single param.

If it got to be more than that it should likely be expanded to a block arrow function in which case it should always use parentheses.

ie.

// good
[1, 2, 3].map(n => n * 2);

// bad
[1, 2, 3].map(n => {
  return n * 2;
});

// good
var fn = (a, b, c) => {
  return a + b + c;
};

// bad
var fn = (a, b, c) => a + b + c;

This is how I’ve seen most people write their code when they submit issues to Babel.

8reactions
nkbtcommented, Apr 4, 2015

I go for readability over consistency in this case. Moreover it is way more often to have only one argument. At least as for my experience. Parenthesis is excessive for 95% of all my cases. To be honest I do lots of D3 and have the only argument for all function almost all the time.

Currying without parenthesis looks awesome too

currying

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expected parentheses around arrow function argument ...
Parentheses around the parameter to an arrow function are optional in ES6 when there's only one argument, but ESLint complains about this by ......
Read more >
Use parentheses for an arrow function parameters
The problem arises from the JavaScript syntax: Arrow function could omit parentheses around its parameters in only one case — when a ...
Read more >
JavaScript Best Practices — Arrow Functions - Level Up Coding
Always Include Parentheses Around Parameters for Clarity and Consistency. Arrow function signatures may skip the parentheses if the signature ...
Read more >
Arrow function expressions - JavaScript - MDN Web Docs
The parentheses can only be omitted if the function has a single simple parameter. If it has multiple parameters, no parameters, or default, ......
Read more >
Arrow functions, the basics - The Modern JavaScript Tutorial
If we have only one argument, then parentheses around parameters can be omitted, making that even shorter. For example:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found