JavaScript - Data Type Conversion

In routine programming style many problem occurs while data type conversion because in other programming language many contradiction into conversion of Data Type.

 

JavaScript tries its best to perform internal conversions to head off such problems, but JavaScript cannot read your mind. If your intentions differ from the way JavaScript treats the values, you won’t get the results you expect.

 

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

But if one of those numbers is a string, JavaScript leans toward converting the other value to a string—thus turning the plus sign’s action from arithmetic addition to joining strings.

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.

But as the 6 is about to be added to the "3" JavaScript lets the "string-ness" of the "3" rule.

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.

While JavaScript provides numerous ways to convert data from one type to another, it is appropriate at this stage of the tutorial to introduce you to the two most common data conversions:
            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.

The JavaScript language provides two built-in functions to convert string representations of numbers to true numbers:
            parseInt() and parseFloat().

There is a difference between integers and floating-point numbers in JavaScript. Integers are always whole numbers, with no decimal point or numbers to the right of a decimal. Floating point numbers, on the other hand, have fractional values to the right of the decimal.

By and large, JavaScript math operations don’t differentiate between integers and floating-point numbers:

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.

As we saw that, JavaScript gravitates toward strings when faced with an expression containing mixed data types. Even so, it is good practice to perform data type conversions explicitly in your code to prevent any potential ambiguity.

The simplest way to convert a number to a string is to take advantage of JavaScript’s string tendencies in addition operations. By adding an empty string to a number, you convert the number to its string equivalent:
            ("" + 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.

A string is a JavaScript object that has properties associated with it. One of those properties is the length property, which evaluates to the number of characters in the string.

Therefore, the length of the string "2500" is 4. Note that the length value is a number, not a string.