In JS, in general, the data types are divided into two categories, one is the original value type, and the other is the reference type. The original value types include: string , Numeric (number) , boolean (Boolean) , and two special values null and the undefined . Reference type of the main refers to the object, in JS, the scope of the object is very wide, and even JS in everything is the object of the argument. objects can be categorized into objects (object) and more specific arrays (array) , functions , and so on. Note that the typeof operator in JS returns only the type: Sting, Number, Boolean, object, function, undefined, typeof null returns an object, However, it is generally assumed that null is not part of the object.
About JS is not everything is the object of this sentence, in fact, is controversial. For example, we can call a method directly on a string literal defined by single or double quotation marks:
' This is a string '. CharAt (0);
This statement is capable of executing and getting the correct results. The string is really an object, but the problem is that the literal string is not counted by the constructor but not the object. Does this mean that this statement is not the method that is called on the string literal, but the object that can invoke the method. Yes, that's what we see, but what about the facts?
The fact is that a string defined by a literal is not an object, or at most it is a pseudo-object, because when a method or property is called on a string literal, the program silently creates a temporary string object that is equal to the value of the literal, and then invokes the property or method on the temporary object. Finally, the result is returned, and the temporary string object will be destroyed as soon as it completes its mission.
There is also a strong proof that you can add a property to a string literal, or change a property value, but the result is not successful:
VARs = ' This is a string '; S.bar = ' hello ';//Add a Property alert (S.bar) to the string;//The result is undefined, not ' hello '
The reason for getting undefined is that the S.bar attribute is added to the temporary string object that the program secretly created, but we can't get a reference to the temporary character creation object, and the temporary string object is soon destroyed. So it doesn't make sense to add attributes to string literals or to change the value of a property, because it's not affected at all.
Digital literals and Boolean literals are similar in principle and are not discussed here.
The following goes to the point---js in the various data types between the conversion.
First, convert to a Boolean value
The program is in the IF statement and | |, &&,! The expression is automatically converted to a Boolean value in the context of logical judgment.
There are two ways to manually convert something to a Boolean value: 1, use!! ; 2, use a Boolean (), remember not to add new before;
1. Converts a number to a Boolean value
Except that 0 is converted to false, all from his number will be converted to true, and NaN is always converted to false
2. String converted to Boolean value
This is simpler, except that the empty string is converted to false, and all strings are converted to true
3. Convert other types to Boolean values
Undefined and null will be converted to false, and any objects (including arrays) and functions will be converted to true, remembering that any
Varo =newboolean (FALSE); alert (o);//Convert to string, the result is Falsealert (!! o);//Convert to a Boolean value, the result is true
second, convert to string
There are two ways to cast a certain thing into a string:
' + x//method One, add an empty string with it string (x)//Method two, using a string constructor without new
1. Convert numbers into strings
This is nothing to say, the numbers are converted to strings as they are, but the numbers represented by scientific notation (that is, with E) are converted into strings of real numbers that represent them internally.
It is also important to note that when you use the two-dollar plus operator, if one of the two operands is not a number, the string connection operation is performed instead of the mathematical addition operation, and two operands are converted to a string. When NULL is added to a number, it does not concatenate strings, but instead converts null into a mathematical element.
[]+1+3//result is 13[1]+3 //result is 13null+1+3//result is 4
2. Convert other types into strings
When an object or function is converted to a string, their toString () method is called to convert, by default Object.prototype.toString and Function.prototype.toString, But they can be overridden by our own custom ToString method. When converting a function into a string, it is not necessary to show the source code of the function, the result of the Function.prototype.toString method depends on how it is implemented by its environment.
Three, converted into digital
Mathematical operators other than the plus sign perform operations that convert to numbers. There are two ways to force a thing into a number:
+x//uses a unary plus operator, which is the quickest way to x-0 or x*1//another form number (x)//convert using a numeric constructor without new
1. Convert strings into numbers
In addition to the empty string will be converted to 0, if the string is the correct form of digital writing, then can be smoothly converted to the corresponding number, whether it is a decimal, scientific count or octal, 16 binary form, and so on. But if you get mixed up with something else that doesn't make a number or does not conform to the rules of digital writing, it will be converted to Nan.
Nan refers to the meaning of not numbers, and any numeric number that is calculated with Nan is Nan,nan or even not equal to itself.
2. Convert other types into numbers
Objects and functions are always converted to Nan, and undefined are converted to Nan, but Null is converted to 0
The table above omits the case of the array. The array is first converted to a string and then converted to a number.
Alert (+[]); The result is 0alert (+[1]);//result is 1alert (+[1,2]);//result is Nanalert (+newarray (1));//result is 0alert (+newarray);//result is Nan
3, two number conversion functions
parseint
Parsefloat
Data type conversions look simple, but some things can be confusing, and it takes some effort to really master it.
Data types in JavaScript convert those things