Home > front end >  The transformation of the data type
The transformation of the data type

Time:09-27

Data type conversion is divided into two kinds: automatic conversion and forced into
A. Automatic conversion: is a computer program, automatically convert
Boolean type automatic transformation: 1. When performing the if judgment, other data types will automatically be converted to Boolean type
The transformation principle:
1> Into false: 0 'undefined null NaN the five kinds of situations into false
Such as: "' an empty string into false

2> In addition to the above five kinds of, all the other will be transformed into true
Such as:

2. A string of automatic conversion: when performing string concatenation, other data types will be converted to string type.
Perform string concatenation, splicing symbol + number on both sides should be string type
If it is other types JavaScript program, will automatically be converted to string type, and then perform stitching
The transformation principle:
1) Boolean values true into string 'true'
A Boolean false into string 'false'
Undefined undefined into string ' '
Unll into string 'null'
Value is converted to the corresponding numerical analysis can be converted to digital string
2) three special transformation:
1> Array - & gt; The contents of the [] can be converted into a string of the form, for Mosaic
Such as: var str9='Beijing' + [1, 2, 3, 4, 5];
The console. The log (str9);
2> Object - & gt; Any object, any content, will translate into [object object] the content of the fixed form
Such as: var str10='Beijing' + {name: "zhang SAN, age: 18, sex: 'male'};
The console. The log (str10);
3> Function - & gt; All of the program code can be converted into a string
Such as: var str11='Beijing' + function fun () {the console. The log (123)};
The console. The log (str11);
3. The automatic conversion of numerical: (when performing mathematical operations will trigger automatic conversion of data types)
The transformation principle:
1> Boolean type: true - & gt; 1
False - & gt; 0
Undefined: into NaN
Null: into 0
2> String:
If the entire string, it is pure digital string, or conform to the scientific notation - & gt; Into the corresponding numerical
If the string contains does not conform to the content of the digital specification - & gt; Into NaN
3> Array, object, function
If it is + plus: enforce a string concatenation effect
According to the principle of these data types into a string to convert
If it is other forms of operation: the execution results are NaN

Forced conversion: the programmer is forced to complete the transformation of

1. The Boolean type coercing: is the use of the methods defined in the JavaScript/Boolean function (data/variables)
Note: Boolean () this method does not change the variable stored in the original number
Transformation principle and automatic conversion principle is the same:
False: 0, ', undefined, null, NaN
True: other data are transformed into true
2. The string force transformation:
Method 1: variable. ToString (hexadecimal type)
Convert numerical forced into strings, and can be set into hexadecimal
Before. The toString (), not directly write value, must be written as a variable in the form of a
Hexadecimal common value is 2 August 16
Can be set or hexadecimal scope is: the range of 2-36 numerical + 26 English letters (10)
Is at least 2 base, up to 36 hexadecimal
Method 2: String/data (variables)
A variable or data can be converted into a string
Principle, according to the principle of automatic conversion to perform
Will not change the variables stored in the original number
Such as: var e5 int=2;
The console. The log (String (int));
The console. The log (int);
Numerical forced into
3.Method 1: the Number (variable/Number)
Turn other type forced into numeric types, principle is the same as the automatic conversion choose
Method 2: parseInt (variable/data)
Is to obtain variable or data the integer part of
Since data left, parsing for integer content
1> Array implementation, is to obtain the numerical part, also is not a part of the [].
Such as: 1, 2, 3, 4, 5 integer part is 1, 1 is a comma after comma is not the integer, after the part is not integer.
For the first numerical integer part, if you have get, if not, the result is NaN
Example: the console. The log (parseInt ([1, 2, 3, 4, 5]));//the result is 1
The console. The log (parseInt ([null, 2, 5-tetrafluorobenzoic]));//the result is NaN
2> If the integer is directly obtained, if it is a floating point number, or scientific notation, obtain the integer part
Such as: the console. The log (parseInt (100));//integer is directly obtained
The console. The log (parseInt (0.0123));//floating point Numbers is to obtain the integer part
The console. The log (parseInt (e3) 3.123456);//scientific notation is parsed, obtain the integer part
3> String
If it is pure digital string
The console. The log (parseInt (' 100 '));//same as the result of the digital
The console. The log (parseInt (' 0.0123 '));//same as the result of the digital
If it is a string, starting from the left, to find the integer part, the first is 3, is the second point, the point is not the integer, so the integer part is 3
The console. The log (parseInt (' 3.123456 e3));//the result is 3
The console. The log (parseInt (' 3 ABC '));//the result is 3
The console. The log (parseInt (' 3.123 '));//the result is 3
Method 3: parseFloat (variable/number)

1> For floating-point part
Such as: the console log (parseFloat (true));
The console. The log (parseFloat (false));
The console. The log (parseFloat (null));
The console. The log (parseFloat (undefined));
The console. The log (parseFloat ({name: "zhangsan"}));
The console. The log (parseFloat (function fun () {the console. The log (' ABC ')}));
Above the output are NaN
2> Values (integers, floating point number, will complete access)
Such as: the console. The log (parseFloat (100));//output is 100
The console. The log (parseFloat (100.1234));//output is 100.1234
The console. The log (parseFloat (e3) 1.234567);//output is 1234.567
3> String (from the string to the left of parsing conform to the floating point number)
Such as: the console log (parseFloat (' 100 '));//same as the result of the digital output is 100
The console. The log (parseFloat (' 0.0123 '));//same as the result of the digital output is 0.0123

The console. The log (parseFloat (' 3.123456 e3));//science and technology will parse the output is 3123.456
The console. The log (parseFloat (' 3.1223 "ABC"));//output is 3.1223
The console. The log (parseFloat (' 3.123 '));//output is 3.123


Above is the transformation of the two kinds of data type conversion mode: automatic conversion and forced conversion, welcome everybody to discuss oh ~ ~ if you have any questions
  • Related