# JavaScript Type Conversion
● JavaScript Data Types
In JSP there are 5 different date types that con contain values.
- string
- number
- boolean
- object
- function
And, there are 3 types of objects.
- Object
- Date
- Array
Last, 2 data types that can not contain values.
- null
- undefined
● The typeof Operator
Programmer can use the typeof operator to find the data type of a JSP variable.
● The Data Type of typeof
The typeof operator is not a variable. It is an operator. Operators do not have any data type. But, the typeof operator always returns a string.
● The constructor Property
The constructor property returns the constructor function for all JSP variables.
● JavaScript Type Conversion
JavaScript variables can be converted to a new variable and another data types.
- By the use of a JavaScript function
- Automatically by JavaScript itself
● Converting Numbers to Strings
The global method String() can convert numbers to strings.
It can be used on any type of numbers, literals, variables, or expressions.
The number Method toString() does the same.
It can be used on any type of numbers, literals, variables, or expressions.
The number Method toString() does the same.
● Converting Strings to Numbers
The global method Number() can convert strings to numbers.
Strings containing numbers convert to numbers.
Empty strings convert to 0.
Anything else converts to NaN ( Not A Number ).
Strings containing numbers convert to numbers.
Empty strings convert to 0.
Anything else converts to NaN ( Not A Number ).
● The Unary + Operator
The unary + operator can be used to convert a variable to a number.
If the variable can not be converted, it will still become a number, but with the value NaN.
If the variable can not be converted, it will still become a number, but with the value NaN.
● Converting Booleans to Numbers
The global method Number() can also convert booleans to numbers.
● Automatic Type Conversion
When JavaScript tries to operate on a "wrong" data type, it will try to convert the value to a "right" type.
https://www.w3schools.com/js/js_type_conversion.asp |
● Automatic String Conversion.
JavaScript automatically calls the variable's toString() function when programmer try to "output" an object or a variable.
No comments:
Post a Comment