JavaScript Data type conversions

Source: Internet
Author: User

JS is a weakly typed language1. When declaring a variable, you do not need to specify the data type stored by the variable2. Dynamically determine the type of data when assigning a valueThe same variable can hold different types of values successively3.js will automatically convert the type of data when needed two major types of conversions: implicit conversion and casting  Implicit conversionsNo programmer intervention required, JS auto-complete type conversionIn arithmetic calculations, all data is converted by default to a number, and then in the + operation, when the string is encountered, + becomes a string concatenation, the other is not a string of data, is automatically converted to a string special case: If the data cannot be converted to a numeric type, it will be converted to Nan, Represents a number that is not a number (not a valid number) Nan does not equal, is not greater than, not less than, any value, including Nan Nan participates in any arithmetic result that is always a Nan expression: a formula that consists of variables, data, and operators, by default, from Left to right, 22 operation, any expression has an operation result any expression can use a numeric + string as a value: string number + Boolean: Number (true=1,false=0) string + Boolean: String (boolean value =true/ False) Boolean value + Boolean: Number (true=1,false=0) Example:var n1=2, n2=3, s1= "2", s2= "3", B1=true, B2=false, Str;console.log (N1+N2); 5
Console.log (N1+S2); "23"
Console.log (S2-S1); 1
Console.log (S1-N2); -1
Console.log (N1+N2+S2); "53"
Console.log (N1+S2+N2); "233" Console.log (S2-B1); 2
Console.log (S2+B1); "3true"
Console.log (S1*B2); 0
Console.log (S1+STR); "2undefined"
Console.log (S2-STR); NaN
Console.log (N1*STR); NaN Forced Conversionsprogrammers actively invoke specialized functions to perform conversionsWhen to use: the type of automatic conversion, not the type you wantconvert any type to a string type
x.tostring (); X is not undefined and null can be usedString (x); equivalent to an implicit conversion converts a non-string type to a numeric typeNumber (x)To convert a non-string to a number, rather than an implicit conversion, to convert a string type to a numeric typeparseint (str); starts with the first character, reads each number sequentially, stops automatically skipping the empty character at the beginning of the first non-numeric character, and does not recognize when the decimal point is used: Converts a string to an integerparsefloat (str); recognize when the first decimal point is used: Convert a string to a decimal to convert any type to a Boolean typeBoolean (x); equivalent to an implicit conversion only 5 values are converted to false, the rest is true "" NaN undefined null 0 fast conversion: Any type to STRING:X + "" Any type of turn boolean:!! X prompt ();Hint information var salary = prompt ("Please enter your salary");//Enter 5000var mysalary = Salary+1000;//console.log ("Your salary after salary is (error):" + mysalary);// 50001000var Mysalary2 = number (Salary) +1000;console.log ("Your salary after salary is (correct):" + mysalary2);//6000 * * * * All data fetched (returned) from the page is a string, Must first be converted in the calculation

JavaScript Data type conversions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.