This article mainly introduces the data type conversion method in JavaScript, which is the basic knowledge in JavaScript beginners. If you need it, you can refer to JavaScript variables to be converted to new variables or other data types:
- By using JavaScript Functions
- Automatic conversion through JavaScript
Convert a number to a string
The global method String () can convert numbers into strings.
This method can be used for any type of numbers, letters, variables, expressions:
Instance
String (x) // convert variable x to String and return String (123) // convert number 123 to String and return String (100 + 23) // convert the numeric expression to a string and return
The Number method toString () also has the same effect.
Instance
x.toString()(123).toString()(100 + 23).toString()
Convert a Boolean value to a string
The global method String () can convert a Boolean value to a String. String (false) // returns "false" String (true) // returns the "true" Boolean method toString () with the same effect. False. toString () // return "false" true. toString () // return "true"
Convert a date to a string
The global method String () can convert a date to a String.
String (Date () // returns Thu Jul 17 2014 15:38:19 GMT + 0200 (W. Europe Daylight Time)
The Date method toString () also has the same effect.
Instance
Date (). toString () // return Thu Jul 17 2014 15:38:19 GMT + 0200 (W. Europe Daylight Time)
Converts a string to a number.
The Global Method Number () can convert a string to a Number.
A string contains numbers (such as "3.14") and converts them to numbers (such as 3.14 ).
Convert an empty string to 0.
Other strings are converted to NaN (not a number ).
Number ("3.14") // returns the 3.14 Number ("") // returns the 0 Number ("") // returns the 0 Number ("99 88") // returns the NaN
Unary operator +
Operator + can be used to convert a variable to a number:
Instance
Var y = "5"; // y is a string var x = + y; // x is a number
If the variable cannot be converted, it will still be a number, but the value is NaN (not a number ):
Instance
Var y = "John"; // y is a string var x = + y; // x is a number (NaN)
Convert a Boolean value to a number
The Global Method Number () can convert a Boolean value to a Number.
Number (false) // return 0 Number (true) // return 1
Convert a date to a number
The Global Method Date () can convert a Date to a number.
D = new Date (); Number (d) // 1404568027739 is returned.
The date method getTime () has the same effect.
D = new Date (); d. getTime () // return 1404568027739
Automatic Conversion Type Conversion
When JavaScript tries to operate on a "error" data type, it is automatically converted to a "correct" data type.
The following output is not what you expected:
5 + null // return 5 because null is converted to 0 "5" + null // return "5 null" because null is converted to "null" "5" + 1 // returns "51" because 1 is converted to "1" "5"-1 // returns 4 because "5" is converted to 5
Automatically convert to string
When you try to output an object or a variable, JavaScript will automatically call the toString () method of the variable:
Document. getElementById ("demo "). innerHTML = myVar; // if myVar = {name: "Fjohn"} // toString to "[object Object]" // if myVar = [1, 2, 3, 4] // toString to "1, 2, 3, 4" // if myVar = new Date () // toString to "Fri Jul 18 2014 09:08:55 GMT + 0200"
Numbers and boolean values are also frequently converted:
// If myVar = 123 // toString to "123" // if myVar = true // toString to "true" // if myVar = false // toString" false"