Drongo_III Posted November 11, 2013 Share Posted November 11, 2013 Hello I've recently been working with some JS code that has a return statement that will return one or other of a value. A terrible example... function blah(){ return 1==2 || 2==2; } console.log(blah()); Obviously this is just to illustrate the point. I have to admit this was new on me and I would usually do some evaluation before returning and I didn't realise you could do what the code above does. I cant really find anything on this so I have some basic questions: Does this simply return the first of the two that resolves to true? Can it only be used with boolean statements? Can this be used in other languages? And does it have a name? Quote Link to comment https://forums.phpfreaks.com/topic/283825-basic-question-on-returning-one-of-two-values/ Share on other sites More sharing options...
dalecosp Posted November 11, 2013 Share Posted November 11, 2013 Given that it's confusing to the reader and could do type coercion (one of Crockford's No-No's), I suggest not using it. If you want to "return TRUE;" you should "return TRUE;" ... right? Quote Link to comment https://forums.phpfreaks.com/topic/283825-basic-question-on-returning-one-of-two-values/#findComment-1457954 Share on other sites More sharing options...
requinix Posted November 11, 2013 Share Posted November 11, 2013 1. a || b will "return" the first value that is equal to true. Since you are doing true || true then it will actually return true. 2. No, you can use it with anything. Some Javascript libraries use statements like var a = b || {}; which will assign "a" to "b" (if set) or an empty object (if not set). 3. In most languages partially: in every other language I can think of a || b will return true or false if any of the arguments are true, rather than returning the first true-ish value. 4. Short-circuit evaluation is where the language only evaluates as little as possible to determine the result of the expression. That is, given a || b where if either argument is true then the expression is true, if "a" is true then it doesn't matter what the value of "b" is so the language won't even bother evaluating it. It allows you to write expressions like denom != 0 && num / denom > 0 without accidentally dividing by zero. Quote Link to comment https://forums.phpfreaks.com/topic/283825-basic-question-on-returning-one-of-two-values/#findComment-1457955 Share on other sites More sharing options...
codefossa Posted November 11, 2013 Share Posted November 11, 2013 1. It's the same as an if statement for what you're doing. || being OR will return if a is true or b is true, then true .. else false. 2. You don't have OR operators in anything but booleans. It seems as if you think you may return either false or true with that, but it's not the case. It will always return true because this or that is true. In your case, the or 2 == 2 will cause it to be true. 3. Yes. 4. An OR operator. All you're doing is returning a statement's result. function example(n) { if (n > 0) return true; return false; } function example(n) { return n > 0; }Both of them do the same thing. In your case, all you did was use an OR in your if statement. if (n > 0 || n == 0)You can return that as well, and get the same result. Quote Link to comment https://forums.phpfreaks.com/topic/283825-basic-question-on-returning-one-of-two-values/#findComment-1457958 Share on other sites More sharing options...
Irate Posted November 12, 2013 Share Posted November 12, 2013 Have you read JavaScript: The Definitive Guide by David Flanagan? You could try using parentheses around the expression followed by a ?: operator (like return (a==b&&c!=d) ? e : f;) such as to not always confuse the reader (given they know about (the) ternary operator and logical operators), but in this case, writing a more elaborate conditional statement with ifs and elses is appropriate, I'd say. Quote Link to comment https://forums.phpfreaks.com/topic/283825-basic-question-on-returning-one-of-two-values/#findComment-1457998 Share on other sites More sharing options...
Drongo_III Posted November 12, 2013 Author Share Posted November 12, 2013 Thanks guys - that's a very definitive answer. I realise it's just an OR statement but I've never tried to return one of two values - I would ordinarily evaluate the statement and return one or the other via a conditional, which seems to be the consensus. But I suppose there might be handy instances where it's worth using. Thanks all. Quote Link to comment https://forums.phpfreaks.com/topic/283825-basic-question-on-returning-one-of-two-values/#findComment-1458040 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.