Javascript Boolean Assignment

Logical operators are typically used with (logical) values. When they are, they return a Boolean value. However, the and operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

Description

The logical operators are described in the following table:

OperatorUsageDescription
Logical AND ()Returns if it can be converted to ; otherwise, returns . Thus, when used with Boolean values, returns if both operands are true; otherwise, returns .
Logical OR ()Returns if it can be converted to ; otherwise, returns . Thus, when used with Boolean values, returns if either operand is .
Logical NOT ()Returns if its single operand can be converted to ; otherwise, returns .

If a value can be converted to , the value is so-called truthy. If a value can be converted to , the value is so-called falsy.

Examples of expressions that can be converted to false are:

  • ;
  • ;
  • empty string ( or ); 
  • .

Even though the and operators can be used with operands that are not Boolean values, they can still be considered Boolean operators since their return values can always be converted to Boolean values.

Short-circuit evaluation

As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:

  • is short-circuit evaluated to false.
  • is short-circuit evaluated to true.

The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect. Also note that the anything part of the above expression is any single logical expression (as indicated by the parentheses).

For example, the following two functions are equivalent.

function shortCircuitEvaluation() {   // logical OR (||)   doSomething() || doSomethingElse();     // logical AND (&&)   doSomething() && doSomethingElse(); } function equivalentEvaluation() {   // logical OR (||)   var orFlag = doSomething();   if (!orFlag) {     doSomethingElse();   }       // logical AND (&&)   var andFlag = doSomething();   if (andFlag) {   doSomethingElse();   } }

However, the following expressions are not equivalent due to operator precedence, and stresses the importance of requiring the right hand operand to be a single expression (grouped if needed by parentheses).

false && true || true // returns true false && (true || true) // returns false

Logical AND ()

The following code shows examples of the (logical AND) operator.

a1 = true && true // t && t returns true a2 = true && false // t && f returns false a3 = false && true // f && t returns false a4 = false && (3 == 4) // f && f returns false a5 = 'Cat' && 'Dog' // t && t returns "Dog" a6 = false && 'Cat' // f && t returns false a7 = 'Cat' && false // t && f returns false a8 = '' && false // f && f returns "" a9 = false && '' // f && f returns false

Logical OR ()

The following code shows examples of the (logical OR) operator.

o1 = true || true // t || t returns true o2 = false || true // f || t returns true o3 = true || false // t || f returns true o4 = false || (3 == 4) // f || f returns false o5 = 'Cat' || 'Dog' // t || t returns "Cat" o6 = false || 'Cat' // f || t returns "Cat" o7 = 'Cat' || false // t || f returns "Cat" o8 = '' || false // f || f returns false o9 = false || '' // f || f returns ""

Logical NOT ()

The following code shows examples of the (logical NOT) operator.

n1 = !true // !t returns false n2 = !false // !f returns true n3 = !'Cat' // !t returns false

Conversion rules

Converting AND to OR

the following operation involving Booleans:

bCondition1 && bCondition2

is always equal to:

!(!bCondition1 || !bCondition2)

Converting OR to AND

the following operation involving Booleans:

bCondition1 || bCondition2

is always equal to:

!(!bCondition1 && !bCondition2)

Converting between NOTs

the following operation involving Booleans:

!!bCondition

is always equal to:

bCondition

Removing nested parentheses

As logical expressions are evaluated left to right, it is always possible to remove parentheses from a complex expression following some rules.

Removing nested AND

The following composite operation involving Booleans:

bCondition1 || (bCondition2 && bCondition3)

is always equal to:

bCondition1 || bCondition2 && bCondition3

Removing nested OR

The following composite operation involving Booleans:

bCondition1 && (bCondition2 || bCondition3)

is always equal to:

!(!bCondition1 || !bCondition2 && !bCondition3)

Specifications

Browser compatibility

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Logical AND ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes
Logical OR ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes
Logical NOT ()Full support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support YesFull support Yes ? Full support Yes

Legend

Full support
Full support
Compatibility unknown
Compatibility unknown

See also

Document Tags and Contributors

 Contributors to this page:wbamberg, zhuangyin, fscholz, Nobleman444, trendy-weshy, hygull, jameshkramer, nmve, Sheppy, ntxt, alexbaumgertner, stevemao, syrrim, cliffordfajardo, tamilvendhank, NexusStar, Mori, Havvy, Delapouite, DuncanMacWeb, azarzycki, HILLOL_DHAR, netinept, keiskimu, fusionchess, ywairong, Potappo, Mgjbot, JeffreyArcand, Dria

 Last updated by:wbamberg,

The general answer to all questions about "why was this language feature not implemented" is that the team who designed the language decided that the benefit didn't outweigh the cost.

Cost can take many forms. It takes time and effort to implement a language feature, but there is also the intrinsic cost of complexity: does the feature make the language more complex or ambiguous, out of proportion to its potential benefit?

The hypothetical operator does something fundamentally different from the other compound assignment operators. While the other operators are purely mathematical in nature, this one is different: it substitutes one value for another (in the context you described).

Given this ambiguity (the operator performs two different functions, depending on context), it's not difficult to see why it was not included in the language. Although you state that changing

to

to perform null coalescing is a valuable feature, the benefit is far less clear to me. If value substitution is to occur, it seems logical (and clearer) to have both values on the right side of the equals sign, to provide a visual indication that such substitution may occur.

C# took a different path, and uses a specific operator for null coalescing.

Categories: 1

0 Replies to “Javascript Boolean Assignment”

Leave a comment

L'indirizzo email non verrà pubblicato. I campi obbligatori sono contrassegnati *