3

Can anyone explain to me what is the difference between these two statements and why the second one does not work and the first one does:

  1. if (finalWord.length > 140) return false; else return finalWord;

  2. (finalWord.length > 140) ? false : finalWord;

4
  • 4
    They do the same thing. The second one doesn't work because you are not returning anything Commented Oct 18, 2018 at 11:04
  • 2
    You miss the return statement in the second example. It should be return (finalWord.length > 140) ? false : finalWord; Commented Oct 18, 2018 at 11:04
  • The second version is missing the return statement. It should be return (finalWord.length > 140) ? false : finalWord; or return finalWord.length > 140 && finalWord; Commented Oct 18, 2018 at 11:05
  • Also, see Conditional operator Commented Oct 18, 2018 at 11:06

1 Answer 1

4

It looks, you miss the return statement.

return finalWord.length > 140 ? false : finalWord;

You could shorten it to

return finalWord.length <= 140 && finalWord;
Sign up to request clarification or add additional context in comments.

2 Comments

I am so stupid :)) I thought that the return statement was implicit when using ternary operator. thanks
@Dito if it was, then you wouldn't be able to write var myVar = someVariable == otherVariable ? "yes" : "no";

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.