The data type of JavaScript is divided into six types, null,undefined,boolean,string,number,object respectively.
object is a reference type, and the other five are of the base type or the original type. We can use the TypeOf method to print out what kind of it belongs to. Variable comparisons of different types have to be typed first, called Type conversions, and type conversions are also called implicit conversions. Implicit conversions usually occur in operator subtraction, equal to, and less than, greater than, etc...
typeof '//string
typeof (one)//number ' one '
< 4//false
First, the basic type of conversion
Let's talk about subtraction:
1. Strings plus numbers, numbers are converted to strings.
2. Digital subtraction String, string into number . if the string is not a pure number, it is converted to Nan. The same is the case for strings minus numbers. Two string subtraction is also converted to a number first.
3. Multiply, except, greater than, less than the conversion is the same as minus.
Implicit conversion +-* = =////--'//-10 '-' one
'//nan
-' 100a '// NaN
//*
10* '//200 '
//200///20/'//2 '/'//2 '
20 '/'/' One '//nan
4. The addition operation order is sensitive
Mixed expressions like this are sometimes confusing because JavaScript is sensitive to the order of operations. For example, an expression:1+2+ "3"; "A"
Since the addition operation is left-bound (i.e. the left-bound law), it is equivalent to the following expression:(1+2) + "3"; "A"
In contrast, the expression:1+ "2" +3; "123" evaluates to the string "123". The left binding law is equivalent to wrapping the addition operation on the left side of the expression in parentheses.
5. Take another look at a group = =
1). undefined equals null
2. String to number when comparing strings and numbers
3). Boolean to number when the number is a Boolean comparison
4. String and Boolean comparisons, both turn the numbers
= =
undefined = null;//true
' 0 ' = 0; True, the string turns
the number 0 = false;//true, the boolean turns number
' 0 ' = false; True, the two turns the number
null = FALSE; False
undefined = = false; False
7 false values:false,0,-0, "", Nan,null, and undefined, all other values are truth
6, NaN, not a number
NaN is a special value that shows that some arithmetic operations (such as the square root of a negative number) do not result in numbers. Method parseint () and parsefloat () return this value when the specified string cannot be resolved. For a function that returns a valid number in a regular scenario, you can also use this method to illustrate its error condition with Number.NaN.
Math.sqrt ( -2)
Math.log ( -1)
0/0
parsefloat (' foo ')
For many JavaScript beginners, the first pitfall is that when you call typeof, the result is usually unexpected:
Console.log (typeof NaN); ' Number '
In this case, Nan does not mean a number, its type is a number. Do you understand?
Because TypeOf returns a string, there are six kinds: "Number", "string", "Boolean", "Object", "function", "undefined
Stay calm, because there's a lot of confusion down there. Let's compare two nan:
var x = math.sqrt ( -2);
var y = Math.log ( -1);
Console.log (x = = y); False
Perhaps this is because we do not use strict equivalence (= = =) operations? Obviously not.
var x = math.sqrt ( -2);
var y = Math.log ( -1);
Console.log (x = = y); False
Compare two nan directly?
Console.log (nan = = Nan); False
Because there are many ways to represent a non-numeric, it makes sense that a non-numeric does not equal another number that is Nan.
But of course, the solution is now available. Let's get to know the global function isNaN:
Console.log (isNaN (NaN)); True
Alas, but isNaN () also has its own many flaws:
Console.log (isNaN (' Hello ')); True
Console.log (isNaN ([' X ']));//True
Console.log (isNaN ({}));//True
This has resulted in many different solutions. One is to take advantage of the non reflective nature of Nan (for example, look at Kit Cambridge's notes)
var i = {
isnan:function (x) {return x!== x;}
}
Fortunately, in the coming ECMAScript 6, there is a Number.isnan () method that provides reliable Nan value detection.
In other words, it returns true only if the argument is a true Nan
Console.log (Number.isnan (NaN)); True
Console.log (Number.isnan (math.sqrt ( -2)));//True
Console.log (Number.isnan (' Hello '));//False
Console.log (Number.isnan ([' X '])); False
Console.log (Number.isnan ({}));//False
Ii. Conversions of reference types
The comparison between basic types is relatively simple. The comparison of reference types and base types is relatively complex, and the reference type is first converted to the base type and then compared to the method described above.
1. Reference type Boolean all is true.
such as an empty array, as long as the object is the reference type, so [] is true. Reference type a number or a string is either valueof () or ToString (), the object itself inherits Valuof () and ToString (), and valueof () and ToString () are also customized. ValueOf () to a string, number, or itself, depending on the object, and the object must be converted to a string with ToString. The generic object calls valueof () by default.
1. When the object turns a number, call valueof ();
2). When the object turns the string, the call ToString ();
Let's take a look at the following example:
0 = = []; True, 0 = = [].valueof (); ---> 0 = 0;
' 0 ' = = []; False, ' 0 ' = = [].tostring (); ---> ' 0 ' = ';
2 = = [' 2 ']; True, 2 = = [' 2 '].valueof (); ---> 2 = ' 2 '---> 2 = 2;
' 2 ' = = [2]; True, ' 2 ' = = [2].tostring (); ---> ' 2 ' = ' 2 ';
[] == ! []; True, [].valueof () = =! Boolean ([])-> 0 = False---> 0 = 0;
When an object is converted to a number, the call to ValueOf () is called ToString (), so I guess the valueof method is this. So the above example 0 = = [] to be changed to the following more reasonable. In any case, [] Finally, it turns into 0.
var valueof = function () {
var str = this.tostring ();///////////////
0 = = []; True, 0 = = [].valueof (); -> 0 = = ' 0 '-> 0 = 0;
Custom valueof () and ToString ();
- The custom valueof () and ToString () are present, and the valueof () is invoked by default.
- If only ToString (), call ToString ();
var a = [1];
a.valueof = function () {return 1;}
a.tostring = function () {return ' 1 ';}
A + 1; 2, valueof () first calls
removing valueof () will call ToString ().
var a = [1];
a.valueof = function () {return 1;}
a.tostring = function () {return ' 1 ';}
A + 1; 2, first call valueof ()
//Remove valueof
delete a.valueof;
A + 1; ' 11 ', call ToString ()
What happens if you return to something else?
var a = [1];
a.valueof = function () {return;}
a.tostring = function () {return 1;};
1-a; NaN
Other objects Call valueof () to different types:
var a = {};
A.valueof (); Object {}
var a = [];
A.valueof (); [] itself
var a = new Date ();
A.valueof (); 1423812036234 number
var a = new RegExp ();
A.valueof (); // /(?:) /Regular Object
Comparison between reference types is a comparison of memory addresses, and there is no need for implicit conversions, which is not much to say.
[] = = []//false address is not the same
var a = [];
b = A;
b = = a//true
2. Explicit conversion
Explicit conversions are simpler and can be directly converted using classes as methods.
Number ([]); 0
String ([]); ”
Boolean ([]); True
There is also a simpler way to convert.
3 + "//String ' 3 '
+ ' 3 '//Number 3
!!' 3 '//True
The above is the entire content of this article, detailed introduction of JavaScript implicit forced conversion, I hope to help you learn.