最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Inline if statement and return - Stack Overflow

programmeradmin4浏览0评论

I have a function that I'm trying to optimize at the moment but I'm running into some issues.

The function will be called a lot of times, so I'm trying to make it so that the returned value is quickly determined and that the next call begins.

(By quickly determined I mean not having a single return statement at the end of the function.)

This is the simplified code :

function myFunction(letr) {
    if (letr === " ") return var letc = " ";
    // ... other checks on letr that will return other values for letc
}

The issue is that the 2nd line doesn't seem to be valid JavaScript.

How can this be written the right way + optimized ?

Thank you in advance !

I have a function that I'm trying to optimize at the moment but I'm running into some issues.

The function will be called a lot of times, so I'm trying to make it so that the returned value is quickly determined and that the next call begins.

(By quickly determined I mean not having a single return statement at the end of the function.)

This is the simplified code :

function myFunction(letr) {
    if (letr === " ") return var letc = " ";
    // ... other checks on letr that will return other values for letc
}

The issue is that the 2nd line doesn't seem to be valid JavaScript.

How can this be written the right way + optimized ?

Thank you in advance !

Share Improve this question asked Jul 4, 2012 at 2:05 m_vdbeekm_vdbeek 3,7748 gold badges49 silver badges79 bronze badges 5
  • you can use ternary operator to optimize your function – Nudier Mena Commented Jul 4, 2012 at 2:08
  • @Nudier you can't return from a ternary ... :) – Alex Commented Jul 4, 2012 at 2:08
  • 1 Why do you need the letc variable? Why not simply do a return " "? – hugomg Commented Jul 4, 2012 at 2:09
  • Yeah, I already use the ternary operator later in this function but when there is no "else" (or ":") statement does it still make sense to use ternary operators ? – m_vdbeek Commented Jul 4, 2012 at 2:10
  • 1. Define var letc; outside of the fn 2. Now you can use the 2nd line as if (letr === " ") return (letc = " "); Encapsulating with parens sets letc to a single space and returns the single space to the fn's return statement - thus letc === " " and the fn also returns " " (which would be a truthy result to whatever interprets it.) – gdibble Commented Jan 14, 2017 at 1:48
Add a comment  | 

3 Answers 3

Reset to default 14

Don't declare a variable for the result, just return the value. Example:

function myFunction(letr) {
  if (letr === " ") return " ";
  if (letr === "x") return "X";
  if (letr === "y") return "Y";
  return "neither";
}

You can also use the conditional operator:

function myFunction(letr) {
  return letr === " " ? " " :
    letr === "x" ? "X" :
    letr === "y" ? "Y" :
    "neither";
}
function myFunction(letr) {
    if (letr === " ") return { letc : " " };

    // ... other checks on letr that will return other values for letc
}

Once you return, the function will be terminated and get the value out for the caller

function myFunction(letr) {
    var letc = " ";
    //Do some thing wit letc;
    if (letr === " ") return letr ;
    // ... other checks on letr that will return other values for letc
}
发布评论

评论列表(0)

  1. 暂无评论