2018-10-12 15:37:13 -04:00
---
title: Comparisons with the & & (logical AND) operator
---
2019-07-24 00:59:27 -07:00
# Comparisons with the && (logical AND) operator
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
· _Combine the two if statements into one statement which will return `"Yes"` if `val` is less than or equal to `50` and greater than or equal to `25`. Otherwise, will return `"No"`._
2019-07-24 00:59:27 -07:00
---
## Hints
### Hint 1
2018-10-12 15:37:13 -04:00
The logical AND (`&&` ) operator compares both statements and returns `true` only if both are true or can be converted to true (truthy).
2019-07-24 00:59:27 -07:00
### Hint 2
2018-10-12 15:37:13 -04:00
Remember that this effect can be also achieved by nesting `if` statements.
2019-07-24 00:59:27 -07:00
---
## Solutions
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
```javascript
function testLogicalAnd(val) {
// Only change code below this line
if (val < = 50 & & val >= 25) {
2019-07-24 00:59:27 -07:00
return "Yes";
2018-10-12 15:37:13 -04:00
}
// Only change code above this line
return "No";
}
// Change this value to test
testLogicalAnd(10);
```
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-12 15:37:13 -04:00
The function first evaluates `if` the condition `val <= 50` evaluates to `true` converting `val` to a number if necessary, then does the same with `val >=25` because of the logical AND (`&&` ) operator; if both return true, the `return "Yes"` statement is executed.
2019-07-24 00:59:27 -07:00
#### Relevant Links
2018-10-12 15:37:13 -04:00
- ["Logical operators" - *MDN JavaScript reference* ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_Operators )
2019-07-24 00:59:27 -07:00
< / details >