简体   繁体   English

typeof === 和 typeof == 的区别

[英]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.

相关问题 (typeof variable ==='boolean')和(typeof variable =='boolean')之间有什么区别? - What's the difference between (typeof variable === 'boolean') and (typeof variable == 'boolean')? 在JavaScript中,typeof x =='y'和typeof x ==='y'之间有什么区别吗? - In JavaScript, is there any difference between typeof x == 'y' and typeof x === 'y'? JavaScript中的“ typeof str”和“ typeof(str)”有什么区别? - What's the difference between “typeof str” and “typeof(str)” in JavaScript? typeof(/ \\ s /)和$ .type(/ \\ s /)之间的区别 - Difference between typeof(/\s/) and $.type(/\s/) 区别:if((typeof OA!='undefined')&& OA)和if(OA) - difference between: if((typeof OA != 'undefined') && OA ) and if(OA) typeof x === 'undefined' 与 if (x) 之间有区别吗 - Is there a difference between typeof x === 'undefined' vs if (x) Coffescript / Javascript类型的差异 - difference in Coffescript / Javascript typeof (typeof variable ===“function”)和jQuery.isFunction()之间有什么区别? - What's the difference between (typeof variable === “function”) and jQuery.isFunction()? JavaScript:typeof(var)==='undefined' 与 =='undefined' 之间的区别? - JavaScript: difference between typeof(var)==='undefined' vs =='undefined'? typeof foo和typeof(foo)/ delete bar和delete(bar)有什么区别,为什么我们都需要? - What's the difference between typeof foo and typeof(foo) / delete bar and delete(bar), and why do we need both?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM