May 06, 2021 JavaScript
6.. Convert the number to a string
9.. Convert the Boolean value to a string
10.. Convert the date to a string
12.. Convert the string to a number
16.. Convert boolean values to numbers
17.. Convert the date to a number
18.. Automatic conversion type Type Conversion
Number() is converted to numbers, String() to strings, and Boolean() to Boolean values.
There are 5 different data types in JavaScript:
3 object types:
2 data types that do not contain any values:
You can use the typeof operator to view the data types of JavaScript variables.
Please note:
If the objects are JavaScript Array or JavaScript Date, we can't tell their type by typeof because they all return Object.
The constructor property returns the constructor for all JavaScript variables.
You can use the constructor property to see if the object is an array (including the string "Array"):
You can use the constructor property to see if the object is a date (including the string "Date"):
JavaScript variables can be converted to new variables or other data types:
The global method String() converts numbers to strings.
This method can be used for any type of number, letter, variable, expression:
The Number method to String() has the same effect.
In the Number Methods section, you can find more ways to convert numbers to strings:
Method | Describe |
---|---|
toExponential() | Converts the value of an object to exponential counting. |
toFixed() | Converts a number to a string, resulting in a number with a specified number of digits after the number of points. |
toPrecision() | Format the number to the specified length. |
The global method String() converts Boolean values to strings.
The Boolean method to String() has the same effect.
The global method String() converts the date to a string.
The Date method to String() has the same effect.
In the Date Method section, you can see more about functions that convert dates to strings:
Method | Describe |
---|---|
getDate() | Returns a day of the month (1 to 31) from the Date object. |
getDay() | Returns a day of the week (0 to 6) from the Date object. |
getFullYear() | The year is returned in four digits from the Date object. |
getHours() | Returns the hour of the Date object (0 to 23). |
getMilliseconds() | Returns the date object's milliseconds (0 to 999). |
getMinutes() | Returns the minutes of the Date object (0 to 59). |
getMonth() | Returns the month (0 to 11) from the Date object. |
getSeconds() | Returns the number of seconds (0 to 59) of the Date object. |
getTime() | Returns the number of milliseconds from January 1, 1970 to the present. |
The global method Number() converts strings to numbers.
Strings contain numbers (such as "3.14") converted to numbers (such as 3.14).
The empty string is converted to 0.
The other strings are converted to NaN (not a number).
In the Number Methods section, you can see more about how strings turn into numbers:
Method | Describe |
---|---|
parseFloat() | Resolve a string and return a float. |
parseInt() | Resolve a string and return an integer. |
Operator plus can be used to convert variables to numbers:
If the variable cannot be converted, it will still be a number, but the value is NaN (not a number):
The global method Number() converts Boolean values to numbers.
The global method Number() converts dates to numbers.
The date method getTime() has the same effect.
When JavaScript tries to manipulate an "wrong" data type, it automatically converts to the "correct" data type.
The following output is not what you expect:
JavaScript automatically calls the variable's toString() method when you try to output an object or a variable:
Numbers and Boolean values are also often converted to each other:
In JavaScript null means "nothing" and is a special type with only one value, representing an empty object reference.
When set to "null", it can be used to empty an object:
Tip: You can use typeof to detect that the null return is object.
In JavaScript, undefined is a variable that does not have a value set.
If a variable does not have a value set, undefined is returned:
JavaScript Standard Reference Tutorial: JavaScript Data Type Conversion