In routine programming style many problem occurs while data type conversion because in other programming language many contradiction into conversion of Data Type.
A case in point is adding numbers that may be in the form of text strings. In a simple arithmetic statement that adds two numbers together, you get the expected result:
Example: 3 + 3 // result = 6
Therefore, in the statement
Example: 3 + "3" // result = "33"
The "string variety" of the second value prevails over the entire operation. The first value is automatically converted to a string, and the result joins the two strings. to the statement:
Example: 3 + 3 + "3" // result = "63"
This might seem totally illogical, but there is logic behind this result. The expression is evaluated from left to right. The first plus operation works on two numbers, getting a value of 6.
The 6 is converted to a string, and two string values are joined to yield "63".
Most of your concern about data types will focus on performing math operations like the ones here. However, some object methods also require one or more parameters of particular data types.
String To Number
Number To String
Converting String To Number:-
As we saw above, if a numeric value is stored as a string—as it is when entered into a form text field—your scripts may have difficulty applying that value to a math operation.
parseInt() and parseFloat().
A number is a number. The only time you need to be cognizant of the difference is when a method parameter requires an integer because it can’t handle fractional values.
To use either of these conversion functions, insert the string value you wish to convert as a parameter to the function. For example, look at the results of two different string values when passed through the parseInt() function:
parseInt("42") // result = 42
parseInt("42.33") // result = 42
Even though the second expression passes the string version of a floating-point number to the function, the value returned by the function is an integer. No rounding of the value occurs here (although other math functions can help with that if necessary).
The parseFloat() function returns an integer if it can; otherwise, it returns a floating-point number as follows:
parseFloat("42") // result = 42
parseFloat("42.33") // result = 42.33
Because these two conversion functions evaluate to their results, you simply insert the entire function wherever you need a string value converted to a number. Therefore, modifying an earlier example in which one of three values was a string, the complete expression can evaluate to the desired result:
3 + 3 + parseInt("3") // result = 9
Converting Number To String:-
We will need for converting a number to its string equivalent than the other way around.
("" + 2500) // result = "2500"
("" + 2500).length // result = 4
In the second example, you can see the power of expression evaluation at work. The parentheses force the conversion of the number to a string.
Therefore, the length of the string "2500" is 4. Note that the length value is a number, not a string.