## 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.
{"ft_lang_label":"__label__en","ft_lang_prob":0.8290472,"math_prob":0.98348665,"size":4873,"snap":"2019-13-2019-22","text_gpt3_token_len":1009,"char_repetition_ratio":0.15506265,"word_repetition_ratio":0.007352941,"special_character_ratio":0.22162938,"punctuation_ratio":0.09364548,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9901471,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-03-24T01:45:19Z\",\"WARC-Record-ID\":\"<urn:uuid:3e792c2b-3dcd-4cf1-8028-440e5dfacfa0>\",\"Content-Length\":\"13750\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:924f47f2-75fa-42bf-ab35-fc081c055ae7>\",\"WARC-Concurrent-To\":\"<urn:uuid:87d65480-daa2-45fe-9916-642e29c8f531>\",\"WARC-IP-Address\":\"69.85.85.163\",\"WARC-Target-URI\":\"http://www.visionsdeveloper.com/tutorial/javascript/javascript-data-type-conversion.jsp\",\"WARC-Payload-Digest\":\"sha1:F25B7PBZ3HRXDNTVP3WNLGDW67N2RALX\",\"WARC-Block-Digest\":\"sha1:54QPDH75IFGS7RTRGESQWN5MJO63IRLC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-13/CC-MAIN-2019-13_segments_1552912203123.91_warc_CC-MAIN-20190324002035-20190324024035-00409.warc.gz\"}"}