Home > front end >  The analysis of basic data types in JavaScript and transformation.
The analysis of basic data types in JavaScript and transformation.

Time:09-27

1, the data type test
In JavaScript, detection data type, use the typeof () method/function
Typeof (variable/data), the results is the data type of the data will be stored in variables,

Typeof is not very accurate representation of each data type, is only applicable to basic data types
General is the use of Boolean type judgment value type string type undefined type


2, the basic data type conversion in JavaScript
(1) other data types, automatically converted to Boolean type
When performing the if judgment, other data types will automatically be converted to Boolean type
Other types into the principle of the Boolean type
Into a false: 0 "undefined null NaN the five kinds of situations into false
Special remind is 0.0 0.00000 0
if (expression) {
executable program
} else {
executable program
};
The if (), accept only you input the true and false two numerical
If you are in the () of the if the input is not true or false content
If the program will automatically will the other data you input into/converted to true or false
Where 0 'empty string undefined null NaN becomes false
True is to perform the if {} false is executed the else {}


(2) a string of automatic conversion
Other types automatically converted to the string
When performing string concatenation, other data types will be converted to string type

Perform string concatenation, splicing symbol + number should be on both sides are string type
If it is other types JavaScript program, will automatically be converted to string type, and then perform stitching

(3) numerical automatically into
Other type automatic into numerical, when performing mathematical operations will trigger the automatic conversion of data types
Conversion principle

Boolean type: true - & gt; 1
false & gt; 0
Undefined: into NaN
Null: into 0
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
  • Related