javascript - Does the arrow function have to always return a value? - Stack Overflow

Many times when using Eslitn or other linting tools I was getting an error like this:Err:Expected t

Many times when using Eslitn or other linting tools I was getting an error like this:

Err: // Expected to return a value at the end of arrow function 

Does arrow functions always have to return a value? If the arrow function has to return a value, what's the reason for this?

For example, in this particular piece of code, I don't need to return anything if a condition is not passed. Anyway my linter yiels at me with the mentioned error.

const getCookie = name => {
  const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length == 2)
    return parts
      .pop()
      .split(';')
      .shift();
};

Many times when using Eslitn or other linting tools I was getting an error like this:

Err: // Expected to return a value at the end of arrow function 

Does arrow functions always have to return a value? If the arrow function has to return a value, what's the reason for this?

For example, in this particular piece of code, I don't need to return anything if a condition is not passed. Anyway my linter yiels at me with the mentioned error.

const getCookie = name => {
  const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length == 2)
    return parts
      .pop()
      .split(';')
      .shift();
};
Share Improve this question asked Dec 14, 2018 at 13:44 p7adamsp7adams 6623 gold badges9 silver badges26 bronze badges 5
  • 1 No, arrow functions do not have to return a value. ESLint is highly opinionated. – Pointy Commented Dec 14, 2018 at 13:45
  • 5 if you return in 1 path, you should return from them all. – Daniel A. White Commented Dec 14, 2018 at 13:46
  • I believe ESLint would give a warning for regular function as well – skyboyer Commented Dec 14, 2018 at 13:47
  • But, at the same time, what are you gaining from it being an arrow function. function getCookie(name) would work just as well. – Andy Commented Dec 14, 2018 at 13:47
  • 1 I'm guessing you're hitting the consistent-return rule. It's not arrow function specific, and as Daniel says - it's designed to make sure you return consistently - ie always or never. – James Thorpe Commented Dec 14, 2018 at 13:55
Add a ment  | 

4 Answers 4

Reset to default 2

No, (arrow) functions don't have to return anything but when function is not supposed to be void (i.e return value is expected), it's a good practice to return from every path/return some kind of default value if every other path fails.

const getCookie = name => {
  const value = `; ${document.cookie}`;
  const parts = value.split(`; ${name}=`);
  if (parts.length == 2) {
    return parts
      .pop()
      .split(';')
      .shift();
  }

  // default return, for example:
  return false;
};

Arrow functions do not have to return a value. Expressions need to return values but statements do not. Try a different linter perhaps.

No. But be aware that even if you dont write a return, the function will silently return undefine.

I would say yes: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Is implicit if you don't use a block for example here:

const implicit = i => i * 2;

But in case you need to use a block then you need to add a return:

const explicit = (i) => {
  if(i > 10) {
    return i * 2;
  }
   return i;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745382146a4625276.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信