[英]Difference between typeof === and typeof ==
What is the difference in using typeof ===
or typeof ==
or is same thing?, since both return the same value.使用typeof ===
或typeof ==
or is same thing 有什么区别?因为两者都返回相同的值。
function main() { var number = 10; console.log(typeof number == 'number'); // true console.log(typeof number === 'number'); // true } main();
The typeof
operator always returns a string. typeof
运算符总是返回一个字符串。 The ===
compares both the value and the type whereas ==
operator only compares the value. ===
比较值和类型,而==
运算符只比较值。
This means both the ==
and ===
comparators will always act the same when you have typeof
and a string on two sides of them.这意味着当你在它们的两侧有typeof
和一个字符串时, ==
和===
比较器的行为总是相同的。
Prefer using the strict equality comparison ===
as is a subset of the ==
.更喜欢使用严格相等比较===
作为==
的子集。 The latter does type conversations like null and undefined being equal which can be surprising.后者确实键入了 null 和 undefined 相等之类的对话,这可能会令人惊讶。
=== makes a strict comparison and only returns true if the compared elements have the same value and type === 进行严格比较,仅当比较的元素具有相同的值和类型时才返回 true
== compares only in terms of values, not type of data == 仅根据值进行比较,而不是数据类型
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.