2018-10-10 18:03:03 -04:00
---
id: 56533eb9ac21ba0edf2244d0
2021-02-06 04:42:36 +00:00
title: Comparison with the Equality Operator
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/cKyVMAL'
forumTopicId: 16784
2021-01-13 03:31:00 +01:00
dashedName: comparison-with-the-equality-operator
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
There are many < dfn > comparison operators</ dfn > in JavaScript. All of these operators return a boolean `true` or `false` value.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
The most basic operator is the equality operator `==` . The equality operator compares two values and returns `true` if they're equivalent or `false` if they are not. Note that equality is different from assignment (`=` ), which assigns the value on the right of the operator to a variable on the left.
2020-04-29 18:29:13 +08:00
```js
function equalityTest(myVal) {
if (myVal == 10) {
return "Equal";
}
return "Not Equal";
}
```
2021-02-06 04:42:36 +00:00
If `myVal` is equal to `10` , the equality operator returns `true` , so the code in the curly braces will execute, and the function will return `"Equal"` . Otherwise, the function will return `"Not Equal"` . In order for JavaScript to compare two different < dfn > data types</ dfn > (for example, `numbers` and `strings` ), it must convert one type to another. This is known as "Type Coercion". Once it does, however, it can compare terms as follows:
2020-04-29 18:29:13 +08:00
```js
1 == 1 // true
1 == 2 // false
1 == '1' // true
"3" == 3 // true
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Add the equality operator to the indicated line so that the function will return "Equal" when `val` is equivalent to `12` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`testEqual(10)` should return "Not Equal"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(testEqual(10) === 'Not Equal');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`testEqual(12)` should return "Equal"
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(testEqual(12) === 'Equal');
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`testEqual("12")` should return "Equal"
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(testEqual('12') === 'Equal');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
You should use the `==` operator
2020-04-29 18:29:13 +08:00
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(code.match(/==/g) & & !code.match(/===/g));
2018-10-10 18:03:03 -04:00
```
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
// Setup
function testEqual(val) {
if (val) { // Change this line
return "Equal";
}
return "Not Equal";
}
testEqual(10);
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
function testEqual(val) {
if (val == 12) {
return "Equal";
}
return "Not Equal";
}
```