Jump to content


Photo

Basic question on returning one of two values


  • Please log in to reply
5 replies to this topic

#1 Drongo_III

Drongo_III

    Advanced Member

  • Members
  • PipPipPip
  • 523 posts

Posted 11 November 2013 - 05:19 PM

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:

 

  1. Does this simply return the first of the two that resolves to true?
  2. Can it only be used with boolean statements?
  3. Can this be used in other languages?
  4. And does it have a name?

Learning something new simply uncovers how much there is left to learn...

One day when I know what I'm talking about all my noob posts will come back to haunt me. But I shall rejoice in the cleansing light of knowledge over ignorance :)

#2 dalecosp

dalecosp

    Advanced Member

  • Members
  • PipPipPip
  • 307 posts
  • LocationJoplin, MO

Posted 11 November 2013 - 05:54 PM

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?


"God doesn't play dice" --- Albert Einstein
"Perl is hardly a paragon of beautiful syntax." --- Weedpacket

#3 requinix

requinix

    Transforming Moderator

  • Moderators
  • 6,238 posts
  • LocationWA

Posted 11 November 2013 - 05:55 PM

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.

#4 Xaotique

Xaotique

    Advanced Member

  • Members
  • PipPipPip
  • 572 posts
  • LocationPennsylvania, United States
  • Age:22

Posted 11 November 2013 - 06:06 PM

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.

#5 Irate

Irate

    Advanced Member

  • Members
  • PipPipPip
  • 358 posts
  • LocationHamburg, Germany
  • Age:17

Posted 12 November 2013 - 06:12 AM

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.


Quod placet mihi non placeat tibi. - What I think to be good must not always equal your perception of it.

I am not perfect. I try a lot with the code I provide and I don't guarantee for it to work as I have mostly no option to test it on my mobile phone. I do apologize for any inconvenience I caused, but if I do happen to have helped, liking my posts or marking them as to have solved or answered your question would be nice.

#6 Drongo_III

Drongo_III

    Advanced Member

  • Members
  • PipPipPip
  • 523 posts

Posted 12 November 2013 - 03:09 PM

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.


Learning something new simply uncovers how much there is left to learn...

One day when I know what I'm talking about all my noob posts will come back to haunt me. But I shall rejoice in the cleansing light of knowledge over ignorance :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com