39 lines
1.0 KiB
Markdown
Raw Normal View History

2018-10-12 15:37:13 -04:00
---
title: Use the Conditional (Ternary) Operator
---
## Use the Conditional (Ternary) Operator
### Problem explanation:
_Use the `conditional operator` in the `checkEqual` function to check if two numbers are equal or not. The function should return either true or false._
2018-10-12 15:37:13 -04:00
#### Hint 1
Remember that the "traditional" `if...else` syntax can be re-written using the conditional operator (`condition ? statement if true : statement if false;`)
> _try to solve the problem now_
>
## Spoiler alert!
**Solution ahead!**
## Code solution:
2018-10-12 15:37:13 -04:00
```javascript
function checkEqual(a, b) {
return a === b ? true : false;
2018-10-12 15:37:13 -04:00
}
```
· Run code at [repl.it](https://repl.it/@AdrianSkar/Basic-JS-Ternary-operator).
### Code explanation
- The function checks if the `condition` before the interrogation sign (`?`) is true, and if so, executes the `true` statement. Otherwise, it returns `false`.
### Resources
- ["Conditional (ternary) operator" - *MDN JavaScript reference*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator)