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.

Conditional expression in Arrow function

See original GitHub issue

The no-confusing-arrow rule is enabled, so the following syntax is an error: const fn = a => a ? 1 : 2;

The arrow-body-style rule is enabled, so the following syntax is an error too: const fn = a => { return a ? 1 : 2; };

So the only way to write it following the rules is the following one: const fn = a => { if (a) return 2; return 3; };

Is this correct? Or is there another way to write the same stuff in a different way?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6

github_iconTop GitHub Comments

5reactions
tough-griffcommented, Apr 26, 2016

The allowParens option is set to true, so

const fn = a => (a ? 1 : 2);

should not cause a linter error.

0reactions
TejalGujarcommented, Jul 22, 2020

Got it

On Sun 19 Jul, 2020, 1:14 AM Jordan Harband, notifications@github.com wrote:

@tejalbadgujar https://github.com/tejalbadgujar if your function doesn’t use this or arguments or super, and isn’t newed, then there’s really no difference. All three of those functions could be arrows or normal ones, just the same.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/airbnb/javascript/issues/856#issuecomment-660532601, or unsubscribe https://github.com/notifications/unsubscribe-auth/ANS6YA5O5BJJQVCLU75UEBTR4H3SBANCNFSM4CB7EIGQ .

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use if-else condition in arrow function in JavaScript?
An arrow function can simply be seen as a concise version of a regular function, except that the return is implied (among a...
Read more >
Arrow function expressions - JavaScript - MDN Web Docs
An arrow function expression is a compact alternative to a traditional function expression, with some semantic differences and deliberate ...
Read more >
How to Use ES6 Arrow Functions in JavaScript
Arrow functions can also be an excellent choice when you want to use conditional statements inside a function block. The following script defines...
Read more >
Arrow functions, the basics
This creates a function func that accepts arguments arg1..argN , then evaluates the expression on the right side with their use and returns...
Read more >
It's possible to use if statement in a arrow function with no " ...
The ternary if would work in the shorter syntax, but you have to return something in the else case since its required in...
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