View comments | RSS feed

|| (logical OR)

Availability

Flash Player 4.

Usage

expression1 || expression2

Parameters

expression1, expression2 A Boolean value or an expression that converts to a Boolean value.

Returns

A Boolean value.

Description

Operator (logical); evaluates expression1 (the expression on the left side of the operator) and returns true if the expression evaluates to true. If expression1 evaluates to false, expression2 (the expression on the right side of the operator) is evaluated. If expression2 evaluates to false, the final result is false; otherwise, it is true.

If you use a function call as expression2, the function will not be executed by that call if expression1 evaluates to true.

The result is true if either or both expressions evaluate to true; the result is false only if both expressions evaluate to false. You can use the logical OR operator with any number of operands; if any operand evaluates to true, the result is true.

For more information, see "Operator precedence and associativity" in Using ActionScript in Flash.

Example

The following example uses the logical OR (||) operator in an if statement. The second expression evaluates to true, so the final result is true:

var x:Number = 10;
var y:Number = 250;
var start:Boolean = false;
if ((x>25) || (y>200) || (start)) {
   trace("the logical OR test passed"); // output: the logical OR test passed
}

The message the logical OR test passed appears because one of the conditions in the if statement is true (y>200). Although the other two expressions evaluate to false, as long as one condition evaluates to true, the if block executes.

The following example demonstrates how using a function call as expression2 can lead to unexpected results. If the expression on the left of the operator evaluates to true, that result is returned without evaluating the expression on the right (the function fx2() is not called).

function fx1():Boolean {
   trace("fx1 called");
   return true;
}
function fx2():Boolean {
   trace("fx2 called");
   return true;
}
if (fx1() || fx2()) {
   trace("IF statement entered");
}
/* The following is sent to the Output panel:
   fx1 called
   IF statement entered
*/

See also

! (logical NOT), != (inequality), !== (strict inequality), && (logical AND), == (equality), === (strict equality)


Comments


Fumio Nonaka said on May 23, 2005 at 12:06 AM :
[Returns]
A value of expression1 or expression2.

[Description]
Evaluates expression1 (the expression on the left side of the operator)
and returns it if the expression evaluates to true; otherwise, returns
expression2 (the expression on the right side of the operator). Thus,
when used with Boolean values, if both expression1 and expresstion2
evaluate to false, the final result is false; otherwise, it is true.

See: ECMA-262 "11.11 Binary Logical Operators"
http://bclary.com/2004/11/07/ecma-262#a-11.11

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001163.html