Javascript type conversion. Directory:
1: pseudo object
2: convert to a string
3: Numeric to string
4: convert to a number
5: Convert to Boolean
6: difference between Number () and parseInt ()
7: differences between String () and toString ()
1: pseudo object
Pseudo object: javascript is a very interesting language. Even basic types are also pseudo objects, so they all have attributes and methods.
The type of variable a is a string, and its length is obtained by calling its pseudo object attribute length.
Script
Var a = "hello javascript ";
Document. write ("the type of variable a is:" + (typeof ));
Document. write ("
");
Document. write ("the length of variable a is:" + a. length );
Script
Running effect:
The type of variable a is string.
The length of variable a is 16.
2: convert to a string
Either Number, Boolean, or String has a toString Method for conversion to a String.
Script
Var a = 10;
Document. write ("Number" + a + "converted to string" + a. toString ());
Document. write ("
");
Var B = true;
Document. write ("Boolean" + B + "convert to string" + B. toString ());
Document. write ("
");
Var c = "hello javascript ";
Document. write ("string" + c + "converted to string" + c. toString ());
Document. write ("
");
Script
Running effect:
Convert Number 10 to string 10
Convert Boolean true to string true
Convert string hello javascript to string hello javascript
3: Numeric to string
The default mode and baseline mode are used when Number is converted to a string.
Script
Var a = 10;
Document. write ('by default, digits 10 are converted to decimal '+ a. toString (); // default mode, that is, decimal
Document. write ("
");
Document. write (in 'base mode, Number 10 is converted to binary '+ a. toString (2); // base mode, binary
Document. write ("
");
Document. write (in 'base mode, Number 10 is converted to '+ a. toString (8) of octal.); // base mode, octal
Document. write ("
");
Document. write (in 'base mode, Number 10 is converted to hexadecimal '+ a. toString (16); // base mode, in hexadecimal Mode
Document. write ("
");
Script
Running effect:
In default mode, the number 10 is converted to 10 in decimal format.
In base mode, the number 10 is converted to 1010 of the binary.
In base mode, the number 10 is converted to the value 12 of octal.
In base mode, Number 10 is converted to hexadecimal.
4: convert to a number
Javascript provides built-in functions parseInt () and parseFloat (), respectively, and converts them to numbers.
Note: If the converted string is composed of numbers and characters, the parseInt always locates the number until a non-character occurs. So "10abc" will be converted to 10
Question: How much will the string "10abc8" be converted?
Script
Document. write ("string \" 10 \ "converted to numeric:" + parseInt ("10"); // convert integer
Document. write ("
");
Document. write ("string \" 3.14 \ "converted to numeric:" + parseFloat ("444 3.14"); // convert floating point number
Document. write ("
");
Document. write ("Convert string \" 10abc \ "to numeric:" + parseInt ("10abc"); // judge each digit until it is found that it is not the digit
Document. write ("
");
Document. write ("Convert string \" hello javascript \ "to numeric:" + parseInt ("h5555ello javascript"); // return if the number is not included at all
Back to NaN-Not a Number
Document. write ("
");
Script
Running effect:
To convert string "10" to numeric: 10
The string "3.14" is converted to the number: 444
To convert the string "10abc" to a number: 10
The string "hello javascript" is converted to the number NaN:
5: Convert to Boolean
Use the built-in function Boolean () to convert to a Boolean value.
When converting strings:
If it is not null, it is true.
When converting numbers:
True if the value is not 0.
When converting objects:
If it is not null, it is true.
Script
Document. write ("Null String'': "+ Boolean (" "); // Null String
Document. write ("
");
Document. write ("the value after the non-null character 'Hello javascript 'string is converted to a Boolean value:" + Boolean ("hello javascript"); // a non-null string
Document. write ("
");
Document. write ("numeric 0 converted to Boolean value:" + Boolean (0); // 0
Document. write ("
");
Document. write ("number 3.14 converted to a Boolean value:" + Boolean (3.14); // non-0
Document. write ("
");
Document. write ("Empty object null converted to Boolean value:" + Boolean (null); // null
Document. write ("
");
Document. write ("non-Object new Object () value after conversion to Boolean:" + Boolean (new Object (); // The Object exists
Document. write ("
");
Script
Running effect:
Value after converting Null String ''to boolean: false
The value after converting a non-null 'Hello javascript 'string to a Boolean value: true
Value after the number 0 is converted to a Boolean value: false
The value after the number 3.14 is converted to a Boolean value: true
Value After null is converted to boolean: false
Convert non-Object new Object () to a Boolean value: true
6: difference between Number () and parseInt ()
Number () and parseInt () can be used to convert numbers.
The difference is that when the converted content contains non-numbers, Number () will return NaN (Not a Number)
ParseInt () depends on the situation. If it starts with a number, it will return the legal part starting with a number. If it starts with a non-number, it will return NaN
Script
Document. write ("the Number obtained after converting the string '123' through the Number () function:" + Number ("123"); // normal
Document. write ("
");
Document. write ("the Number obtained after converting the string '123abc' through the Number () function:" + Number ("123abc"); // contains non-Numbers
Document. write ("
");
Document. write ("the Number obtained after converting the string 'abc123' through the Number () function:" + Number ("abc123"); // contains non-Numbers
Document. write ("
");
Document. write ("the number obtained after converting the string '123456' using the parseInt () function:" + parseInt ("123"); // normal
Document. write ("
");
Document. write ("the number obtained after converting the string '123abc' through the parseInt () function:" + parseInt ("123abc"); // contains a non-number and returns the valid start value
Digit
Document. write ("
");
Document. write ("the number obtained after converting the string 'abc123' through the parseInt () function:" + parseInt ("abc123"); // contains non-numbers, starting with a non-number,
Return NaN
Document. write ("
");
Script
Running effect:
The Number obtained after converting the string '123' using the Number () function: 123
Number obtained after converting the string '123abc' using the Number () function: NaN
Number obtained after converting the string 'abc123' using the Number () function: NaN
The number obtained after converting the string '123' using the parseInt () function: 123
The number obtained after converting the string '123abc' using the parseInt () function: 123
The number obtained after converting the string 'abc123' using the parseInt () function: NaN
7: differences between String () and toString ()
Both String () and toString () return strings. The difference is that the processing of null
String () returns the String "null"
ToString () will report an error and cannot be executed
Script
Var a = null;
Document. write ('string (null) converts an empty object to a String: '+ String ());
Document. write ("
");
Document. write ('null. toString () will report an error, so subsequent Code cannot be executed ');
Document. write (a. toString ());
Document. write ("This section of text is not displayed because an error is reported in row 5th ");
Script
Running effect:
String (null) converts an empty object to a String: null
Null. toString () will report an error, so subsequent Code cannot be executed
Learn from yesterday, live for today, hope for tomorrow.