Javascript: Conditional expression in Arrow function

Created on 26 Apr 2016  Â·  6Comments  Â·  Source: airbnb/javascript

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?

Most helpful comment

The allowParens option is set to true, so

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

should not cause a linter error.

All 6 comments

The allowParens option is set to true, so

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

should not cause a linter error.

https://github.com/airbnb/javascript#arrows--confusing

Simply copy and paste the code seemed as good practice.

const itemHeight = (item) => { return item.height > 256 ? item.largeSize : item.smallSize; };

itemHeight();

The error reports 1:30 error Unexpected block statement surrounding arrow body arrow-body-style

@medifle thanks, we don't lint the readme, so sometimes the examples can go out of sync.

let fun = (question, yes, no) => {
    if (confirm(question)) yes();
    else no();
};

let asking = fun("Do you agree?", function() { alert("You agreed."); }, function() { alert("You canceled the execution."); });

is this right way or wrong way to explore the arrow ????

@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.

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
.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zurfyx picture zurfyx  Â·  3Comments

graingert picture graingert  Â·  3Comments

surfaceowl picture surfaceowl  Â·  3Comments

xgqfrms-GitHub picture xgqfrms-GitHub  Â·  3Comments

kozhevnikov picture kozhevnikov  Â·  3Comments