Data type and type conversion in 02-JS
1, Data type
Values in JS, whether literal or variable, have explicit types.
(1) Overview
1. 5 basic types
Number number type
String string type
Boolean boolean type, with only two values true and false
Undefined type. There is only one value of this type, which is undefined
Null type, the value of this type has only one null
2. Reference type
(2) Detection of data type
Using the typeof keyword to check the data type is usually used to detect the type of variable, because the type of direct quantity can be seen at a glance
var a = 123; console.log(typeof a); //number
1.number number type
//The variables defined below are of type number <script> var a = 200; var b = -200; var c = 200.235; var d = .5e4; var e = 0xf0; var f = 016; var g = Infinity; var h = NaN; console.log(typeof a); console.log(typeof b); console.log(typeof c); console.log(typeof d); console.log(typeof e); console.log(typeof f); console.log(typeof g); console.log(typeof h); </script>
number type things, all numbers (regardless of positive or negative, integer floating, size, base, infinity, NaN)
2.string string type
var str1 = "Xiao Cai"; var str2 = "666"; var str3 = ""; //An empty string is also a string console.log(typeof str1); console.log(typeof str2); console.log(typeof str3);
3.boolean type
Boolean type (boolean type). Boolean type has only two values true and false, that is, true or false.
var bool = true; console.log(bool ); console.log(typeof bool); //boolean
Note: at this time, true and false are not guided, so they are not strings. At the same time, note that they are also variables. True is a keyword, which has special meaning and represents true and false.
4.undefined type
As mentioned earlier, if only one variable is assigned to var without an initial value, its default value is undefined.
This undefined is self-contained. Both types and values are undefined, and there is only one value of this type.
var un; //Only defined here, no initial value assigned console.log(un); console.log(typeof un); //undefined
(3) Type of variable
The type of variable is related to the assignment, but it has nothing to do with the definition. The var keyword is used for definition.
//Dynamic data type var num = 123; //number console.log(typeof num); num = "ha-ha"; //Changing to string is legal console.log(typeof num);
We say that JS language is called dynamic data type, which determines the type of this variable when assigning values. You can even change the value of this variable to other types of values. The type of variable is automatically detected, which is to check the type of value stored in the variable, not defined.
2, Operators: plus sign+
When both sides of the plus sign are numbers, it is mathematical addition;
As long as one side is a string, the result is string splicing.
console.log("Small" + "CAI"); //Dobbiaco console.log(3 + 2); //5 console.log(3 + "2"); //32
Multiple plus signs, the operation order is from left to right
<script> var a = 3; var b = 2; var c = 5; console.log( a + b + c); //10 console.log( a + b + 'c'); //5c console.log( 'a' + a + b + 'c'); //a32c console.log( 1 + 2 + "a" + "(3 + 4)"); //3a(3 + 4) </script>
The operation is calculated from left to right. The default is digital plus points, but once a string is encountered, it becomes string splicing
3, Data type conversion
JS has some methods that can convert other data types represented in memory to corresponding numeric types.
Therefore, first recognize a statement prompt(), which is similar to alert. It is also a pop-up window, which pops up the input box
prompt("Please enter your phone number","191");
These small functions are called the APIs provided by the program. Each API has its own different syntax.
prompt("prompt text", "default");
The default value can be omitted.
The value entered by the user can be stored in the variable:
var a = prompt("Please enter your phone number","191"); alert("The number you entered is" + a);
Anything received with prompt is a string. Even if the user enters a number, it is also a number of the string.
(1) To number
1.Number method
(1) string "12px"=>NaN ""=>0
(2) boolean true=>1 false=>0
(3) null null=>0
(4) undefined undefined =>NaN
(5) {} {} => NaN
(6)[] [1]=>1, []=>0, [1,2]=>NaN
(7) function Number(function(){})=>NaN
example:
// Calculator // The first step is to let the user enter the first number first var a = prompt("Please enter the first number"); // The second step is to let the user enter a second number var b = prompt("Please enter the second number"); // Step 3 sum var sum = Number(a) + Number(b); // Step 4 pop up results alert(sum);
2.parselnt method
parseInt is to convert a string into an integer without rounding, and directly intercept the integer part. If there is something messy in this string, directly intercept the previous number part.
Therefore, using this method may lose the decimal part
var a = "678"; var b = parseInt(a); //parseInt converts string 678 to number 678 console.log(b); //678 console.log(typeof b); //unmber parseInt("200"); //200 parseInt("200.666"); //200 parseInt("200 hour"); //200 parseInt("200 Years and 2 months"); //200 parseInt("200px"); //200 parseInt("-200.9999"); //-200
parseInt() can not only be converted into an integer, but also be converted into hexadecimal. Any hexadecimal number can be changed into hexadecimal.
Hexadecimal converted strings, separated by commas.
The following calculation results are 15:
parseInt(15,10); parseInt(17,8); parseInt(1111,2); parseInt("0xf",16); parseInt("f",16); parseInt(16,9); parseInt("15e6",10); parseInt("15*6",10);
3.parseFloat method
parseFloat is to convert a string to a floating point number
Try to convert a string into a floating-point number. If there is any mess after the floating-point number, it will be discarded directly.
var a = "123.456.12"; var b = parseFloat(a); console.log(b); //123.456 console.log(parseFloat("123.66 year")); //123.66 console.log(parseFloat("You said 123.66 year")); //NaN
In other words, the number type is number, regardless of integer and floating-point number, but the conversion time is minute.
4.isNaN() method
Function: judge whether a number is NaN
If yes, the result is true; If not, the result is false
The function of isNaN() method is to judge whether the number is NaN. Can it judge other data types?
isNaN(true) ==> false
isNaN('12.5px') ==> true
isNaN(null) ==> false
isNaN(undefined) ==> true
Conclusion:
The isNaN() method will first convert other types of data into digital types, and then judge whether it is NaN or not
(2) To string
If a number is hyphenated with an empty string, it will be automatically converted to a string.
var a = 123; var b = a + ""; console.log(b); console.log(typeof b); //string
(3) To boolean
Boolean() method
In only six cases, the result of Boolean (converted data) is false, and the rest are true.
1.0 digit 0
2.NaN digital NaN
3. "" empty string
4.false Boolean false
5. Undefined type
6. Null type
I don't know much about it...