0

I have below piece of conditional block in Javascript. I optimized it to the level possible by me, but I feel that it can still be more optimized.

Can someone please suggest a better way to write below code.

if(flag) {
  this.flag = flag; /* some assignment*/
  oldSearchType = newSearchType ? newSearchType : oldSearchType; /*Any better way to write this?*/
}

2 Answers 2

4

You could write the second part like this:

oldSearchType = newSearchType || oldSearchType;

which most of us would find clearer. The global if(flag) { condition is clear enough.

Sign up to request clarification or add additional context in comments.

Comments

1

I find the most obvious solution also the most clear:

if (newSearchType) {
    oldSearchType = newSearchType;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.