--- id: 599a789b454f2bbd91a3ff4d title: Practice comparing different values challengeType: 1 videoUrl: '' localeTitle: 练习比较不同的值 --- ## Description
在最后两个挑战中,我们学习了等于运算符( == )和严格相等运算符( === )。让我们快速回顾一下这些运算符的实践。如果要比较的值不是同一类型,则相等运算符将执行类型转换,然后计算值。但是,严格相等运算符将按原样比较数据类型和值,而不将一种类型转换为另一种类型。 例子
3 =='3'//返回true,因为JavaScript执行从字符串到数字的类型转换
3 ==='3'//返回false,因为类型不同并且未执行类型转换
注意
在JavaScript中,您可以使用typeof运算符确定变量的类型或值,如下所示:
typeof 3 //返回'number'
typeof'3'//返回'string'
## Instructions
编辑器中的compareEquality函数使用equality operator比较两个值。修改函数,使其仅在值严格相等时返回“Equal”。
## Tests
```yml tests: - text: 'compareEquality(10, "10")应返回“Not Equal”' testString: 'assert(compareEquality(10, "10") === "Not Equal", "compareEquality(10, "10") should return "Not Equal"");' - text: 'compareEquality("20", 20)应该返回“Not Equal”' testString: 'assert(compareEquality("20", 20) === "Not Equal", "compareEquality("20", 20) should return "Not Equal"");' - text: 您应该使用===运算符 testString: 'assert(code.match(/===/g), "You should use the === operator");' ```
## Challenge Seed
```js // Setup function compareEquality(a, b) { if (a == b) { // Change this line return "Equal"; } return "Not Equal"; } // Change this value to test compareEquality(10, "10"); ```
## Solution
```js // solution required ```