mild proofreading edits (#29854)

removed repeated words and unnecessary punctuation
This commit is contained in:
Rachel Irene Lunan
2019-02-16 11:41:49 -06:00
committed by Christopher McCormack
parent 2981d776c7
commit cafbe33cc7

View File

@ -3,10 +3,9 @@ title: Logical Operators
---
# Logical Operators
Logical operators compare Boolean values and return a Boolean response. There are two types of logical operators - Logical AND and Logical OR. These operators are often written as && for AND, and || for OR.
Logical operators compare Boolean values and return a Boolean response. There are two types of logical operators: Logical AND, and Logical OR. These operators are often written as && for AND, and || for OR.
#### Logical AND ( && )
## Logical AND ( && )
The AND operator compares two expressions. If the first evaluates as ["truthy"](https://developer.mozilla.org/en-US/docs/Glossary/Truthy), the statement will return the value of the second expression.
If the first evaluates as ["falsy"](https://developer.mozilla.org/en-US/docs/Glossary/Falsy), the statement will return the value of the first expression.
@ -22,9 +21,9 @@ undefined && 'abc' //returns the first value, undefined
0 && false //returns the first value, 0
```
#### Logical OR ( || )
## Logical OR ( || )
The OR operator compares two expressions. If the first evaluates as "falsy", the statement will return the value of the second second expression. If the first evaluates as "truthy", the statement will return the value of the first expression.
The OR operator compares two expressions. If the first evaluates as "falsy", the statement will return the value of the second expression. If the first evaluates as "truthy", the statement will return the value of the first expression.
When only involving boolean values (either `true` or `false`), it returns true if either expression is true. Both expressions can be true, but only one is needed to get true as a result.
```js
@ -36,28 +35,15 @@ false || false //returns the second value, false
undefined || 'abc' //returns the second value, 'abc'
0 || false //returns the second value, false
```
#### Short-circuit evaluation
&& and || behave as a short-circuit operators.
In case of the logical AND, if the first operand returns false, the second operand is never evaluated and first operand is returned.
In case of the logical OR, if the first value returns true, the second value is never evaluated and the first operand is returned.
```js
false && (anything) // short-circuit evaluated to false.
true || (anything) // short-circuit evaluated to true.
```
#### Logical NOT (!)
## Logical NOT (!)
The NOT operator does not do any comparison like the AND and OR operators.Moreover it is operated only on 1 operand.
An '!' (exclamation) mark is used for representing the NOT operator.
###### Use of NOT operators
1. conversion of the expression into boolean.
2. returns the inverse of the boolean value obtained in last step.
### Use of NOT operators
- conversion of the expression into boolean.
- returns the inverse of the boolean value obtained in last step.
```js
var spam = 'rinki'; //spam may be equal to any non empty string
@ -74,10 +60,20 @@ var booSpam2 = !spam2;
inverse of which is evaluated to true.
*/
```
#### Tips:
Both logical operators will return the value of the last evaluated expression. For example:
## Short-circuit evaluation
`&&` and `||` behave as a short-circuit operators.
In case of the logical AND, if the first operand returns false, the second operand is never evaluated and first operand is returned.
In case of the logical OR, if the first value returns true, the second value is never evaluated and the first operand is returned.
Some Examples:
```js
false && (anything) // short-circuit evaluated to false.
true && (anything) // short-circuit evaluated to the result of the expression (anything)
true || (anything) // short-circuit evaluated to true.
"cat" && "dog" //returns "dog"
"cat" && false //returns false
0 && "cat" //returns 0 (which is a falsy value)
@ -89,8 +85,6 @@ Both logical operators will return the value of the last evaluated expression. F
Note that where `&&` returns the first value, `||` returns the second value and vice versa.
#### More information:
* [Javascript Truth Table](https://guide.freecodecamp.org/javascript/truth-table)
* [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Logical_Operators)
## Additional Resources
- [Javascript Truth Table](https://guide.freecodecamp.org/javascript/truth-table)
- [MDN](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Logical_Operators)