English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The comparison and conversion rules between different data types in JavaScript are as follows:
1Comparison of objects and boolean values
对象和布尔值进行比较时,对象先转换为字符串,然后再转换为数字,布尔值直接转换为数字
[] == true; //false []转换为字符串'',然后转换为数字0,true转换为数字1,所以为false
2. 对象和字符串比较
对象和字符串进行比较时,对象转换为字符串,然后两者进行比较。
[1,2,3] == '1,2,3' // true [1,2,3]转化为'1,2,3',然后和'1,2,3', so结果为true;
3. 对象和数字比较
对象和数字进行比较时,对象先转换为字符串,然后转换为数字,再和数字进行比较。
[1] == 1; // true `对象先转换为字符串再转换为数字,二者再比较 [1] => '1' => 1 所以结果为true
4. 字符串和数字比较
字符串和数字进行比较时,字符串转换成数字,二者再比较。
'1' == 1 // true
5. 字符串和布尔值比较
字符串和布尔值进行比较时,二者全部转换成数值再比较。
'1' == true; // true
6. 布尔值和数字比较
布尔值和数字进行比较时,布尔转换为数字,二者比较。
true == 1 // true
许多刚接触js的童鞋看到这么多的转换规则就懵圈了,其实规律很简单,大家可以记下边这个图
如图,任意两种类型比较时,如果不是同一个类型比较的话,则按如图方式进行相应类型转换,如对象和布尔比较的话,对象 => 字符串 => 数值 布尔值 => 数值。
另外,我们来看下一些需要"特别照顾"的。
来看一个有趣的题
[] == false; ![] == false;
这两个的结果都是true,第一个是,对象 => 字符串 => 数值0 false转换为数字0,这个是true应该没问题,
第二个前边多了个!,则直接转换为布尔值再取反,转换为布尔值时,空字符串(''),NaN,0,null,undefined这几个外返回的都是true, 所以! []这个[] => true 取反为false,所以[] == false为true。
还有一些需要记住的,像:
undefined == null //true undefined和null 比较返回true,二者和其他值比较返回false Number(null) //0
曾经看到过这样一个代码: (!(~+[])+{})[--[~+"+[]]*[~+[]]+~~!+[]]+({}+[])[[~!+[]*~+[]]] = sb, you can believe it, and I was startled at the time.
When I first encountered it, JavaScript confused me a lot, precisely because of its 'changeable' nature. Below, I will summarize it for you:
7. JS data types: Number, Boolean, String, Undefined, Null, Symbol (es6Newly defined (note: Array is a special Object)
typeof returns7Type: number boolean string object undefined object function
MDN introduces JavaScript as follows: JavaScript is a weakly typed or dynamic language. This means you don't have to declare the type of a variable in advance, and the type will be automatically determined during the execution of the program. This also means that you can use the same variable to store different types of data
8. Let's take a look at some common implicit conversions:
Basic type:
Operator(+,-,*,/,)operation type conversion
+Quotation mark operator:
Summary: When the plus operator is used, String and other types will be converted to String; in other cases, they will be converted to Number type. Note: undefined is converted to Number as 'NaN', and any Number added to NaN will result in NaN.
In other operations, basic types are converted to Number, and String types with characters like: '1a' , 'a1' is converted to NaN just like undefined.
tip:(1)NaN does not equal any value, including itself, so to determine if a value is NaN, you can simply use "!==".
(2) The following can be converted to Boolean type as false: null, 0, '', undefined, NaN, false
(3)number() and parseInt() can both convert objects to Number type, but the Number function is much stricter than the parseInt function. Essentially, if any character cannot be converted to a number, the entire string will be converted to NaN.
Object type
When performing operations with object and primitive types:
var obj = { toString: function(){ return 6; }, valueOf: function(){ return 5; } }; var obj1 = { valueOf: function(){ return 'a'; }, toString: function(){ return 'b'; } };
When dealing with obj,obj1 When converting using Number() and String()
Summary: Number type will call valueOf() first, String type will call toString() first. If the result is an original value, it will return the original value. Otherwise, it will continue to use toString or valueOf, continue to calculate. If the result is not an original value, it will throw a type error;
See the following situation:
Why {} + [] = 0 &63; Because JavaScript considers the first {} as an empty code block at runtime, so it is equivalent to +[] = 0. Also {} +5 = 5, similarly.
Summary:
1. Type errors may be hidden by type conversion.
2. “+“” can represent string concatenation and arithmetic addition, depending on its operands. If one of them is a string, it is a string concatenation.
3. Objects convert themselves to numbers through the valueOf method and to strings through the toString method.
4. Objects with a valueOf method should define a corresponding toString method to return the string representation of equal numbers.
5. When detecting undefined variables, you should use typeof or compare with undefined instead of using truthy operations directly.
Summary
The above-mentioned are some implicit conversions and summaries (recommended) in JavaScript introduced by the editor for everyone, hoping it will be helpful to you. If you have any questions, please leave a message, and the editor will reply to you in time. We are also very grateful for everyone's support for the yelling tutorial!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)