Parenthesis around single arguments in arrow functions
See original GitHub issueAlways 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:
- Created 8 years ago
- Reactions:2
- Comments:5 (2 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
This is how I’ve seen most people write their code when they submit issues to Babel.
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