Jump to content

Recommended Posts

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?

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.

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.

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.