JavaScript data types and type conversions

Source: Internet
Author: User
Tags logical operators

One data type 1) typeof view data type 1.number Digital

Value range: Positive infinity-negative infinity, NaN positive infinity: number.positive_infinity negative Infinity: number.negative_infinity

1 var v =2 alert (typeof V); //  Number
2.string string
1 var v = ""2 alert (typeof V); // string
3.boolean Boolean value True | False
1 var true  2 alert (typeof V); // Boolean
4.undefined Not defined
var v; alert (typeof V); // undefined
5.function function
1 var function (){ }; 2 alert (typeof V); // function
6.object objects: Elements, NULL, arrays, JSON
1 var v =2 alert (typeof V); // Object
Summary: NUMBER/STRING/BOOLEAN/FUNCTION/OBJECT/UNDEFINED2) Standard: Basic type: Number/string/boolean/undefined/null Composite Type: Object Two, Force type conversion 1) Convert to numeric method one: parseint parse to Integer (integer) parsing rules: Starting with the No. 0 bit, a single person's view is a number, if it is not a number, the previous number is extracted, if the No. 0 is not an array, return Nan Note: 1) The second parameter of parseint: parseint (the content to be converted, binary); 2) 0x is a 16-prefix, alert (parseint (' 0xf ')),//153) identifies the first space, + 、-、 0
1Console.log (parseint ("12.724.12.234"));// A2Console.log (parseint ("1a12s"));//13Console.log (parseint (". 123"));//NaN4Console.log (parseint ("s12345"));//NaN5Console.log (parseint (true));//NaN6Console.log (parseint (""));//NaN7Console.log (parseint ("+00123"));//123

Method two: parsefloat resolves to floating-point (decimal) parsing rules: As with parseint, it is not the same, you can parse a decimal point more
 1  console.log (parsefloat ("12.724.12.234")); // 12.724  2  console.log (parsefloat ("1a12s")); // 1  3  console.log (parsefloat (". 123")); // 0.123  4  console.log (parsefloat ("s12345")); // nan  5  console.log (parsefloat (true )); // nan  6  Console.log (Parsefloat ("")); // nan  
Method Three: Number resolution rule: in the digital conversion, the full of the content to be converted to see again, must fully conform to the specifications of the number to go forward, otherwise return NaN special parsing: 1. Boolean: true 1 False 02. String "Null string ' 0" string ' 0 ' is the most open Starts with spaces, + 、-、 0 "+00123"->1233.undefined nan4.function nan5.object nullconsole.log (number (null)),//0 JSON Console.log ( Number ({}));//nan array when the array has only one, it is converted to this bit: the digit, or string, or undefined, will be converted otherwise NaN
1 varv = [];2Console.log (number (v));//03 varv = ["'];4Console.log (number (v));//05 varv = ["'];6Console.log (number (v));//07 varv = [' +00112 '];8Console.log (number (v));// the
2) converting to string strings is actually a direct quotation mark into a string
1 var a = 12323; 2 Console.log (String (a)); // 12323 3 var true ; 4 Console.log (String (a)); // true 5 var function (){}; 6 Console.log (String (a)); // function () {}
3) Convert to Boolean value booleantrue: Non-0 number, non-empty string, function, non-empty object false:0, NaN, empty string, undefined, null NOTE: 1. Null objects cannot be property manipulated
1 var a = []; 2 Console.log (Boolean (a)); // true
C, operator 1) arithmetic operator + Plus, minus, * multiply,/divide,% modulo (remainder), + + 、--2) Assignment operator =, + =,-=, *=,/=,%= a + = b; A=a+b; A-= b; A=a-b; a *= b; A=a*b; a/= b; a=a/b; a *= b; A=A%B;3) Relational operators <, >, <=, >=, = = equals,! = unequal, = = =,!== comparison returns a Boolean value of 4) logical operators && with, | | Or! No?: Ternary operator 1. && Both sides must be true to return true otherwise false
1 var a = 1; 2 var b = 3; 3 console.log (a!=b && a > B); // false
2. | | True on both sides of the side, returns true otherwise false
1 console.log (a!=b | | a > B); // true
3.!
1 console.log (!) ( A! = b)); // false
4. Ternary operator to determine the condition? The return value of the establishment: the return value is not established;
1 var a = 0; 2 var b = 1; 3 var c = a > B? a:b; 4 Console.log (c); // 1
1 //simplifying operations with logical operators2 varA = 1;3 varb = 1;4 functionfn () {5Alert ("Conditions set");6 }7 functionfn2 () {8Alert ("The condition is not valid");9 }Ten  One /*if (A! = b) { A fn (); - } else { - fn2 (); the }*/ -  - //&&: (When the first condition is set, the second item is executed) the right side code is executed when the left side is established, otherwise it returns false directly. - //The first condition is false, and the FN is not executed +a!=b&&fn (); -  + //when the first condition is true, the statement ends, the condition is not set , and the right side is executed. A //The first condition is false, and the fn2 is executed ata!=b| | FN2 ();

Four, implicit type conversion 1) +,++,+=:1. Executes string concatenation 2 when encountering a string. If it can be converted to a number, the numbers are added (number converted to numbers) 3. If you can't turn both left and right sides into numbers, do a string connection
1 var function (){}; 2 var b = 1; 3 alert (A + B); // function () {}1
2)-Minus, * multiply,/divide,% modulo (for remainder) 、--: The left and right sides are turned into numbers, related operations, can not be converted into numbers to return Nan
1 var function (){}; 2 var b = 1; 3 alert (A-B); // NaN
3) = =,! = will do type conversion = = =,!== type is different, return false directly
1 var true ; 2 var b = 1; 3 alert (A = = B); // true
    1. For basic types such as string,number, = = and = = are different
      • 1) comparison between different types, = = Comparison of "converted to the same type after the value" see "Value" is equal, = = = If the type is different, the result is unequal
      • 2) Comparison of the same type, direct "value" comparison, the results of the same
    2. For advanced types such as array,object, = = and = = are no different
      • Make a "pointer address" comparison
    3. The underlying type is different from the advanced type, = = and = = =
      • 1) for = =, convert advanced to base type, and compare "value"
      • 2) = = = = False due to different type
"= =" and "= = =" Are different, one is to determine whether the value is equal, one is the judgment value and the type is exactly equal. The following rule is used to determine whether the two values of the = = = operator comparison are equal
    1. If the two values are of different types, they are not the same.
    2. Same type
      • If two values are numbers and the values are the same, they are equivalent unless one or two of them are Nan (in which case they are not equivalent). The value Nan is never equal to any other value, including itself (strange fellow), to detect whether a value is Nan, you can use the global function isNaN ().
      • If two values are strings, and the characters in the same position in the string are identical, they are identical. If the lengths or contents of the strings are different, they are not equivalent.
      • If two values are Boolean true, or two values are Boolean false, they are equivalent.
      • If two values are null or both are undefined, they are identical.
      • If two values refer to the same object, array, or function, they are identical. If they refer to different objects (arrays or functions), they are not exactly equal, even if the two objects have exactly the same properties, or two arrays have exactly the same elements.
The following rule is used to determine whether the two values of the = = operator comparison are equal
    1. If two values have the same type, then the equivalence of them is detected. If the two values are exactly the same, they are equal. If they are not identical, they are not equal.
    2. If the two values are of different types, they may still be equal. Use the following rules and type conversions to detect their equality
      • If one value is null and the other value is undefined, they are equal.
      • If one value is a number, the other value is a string, the string is converted to a number, and the value is compared with the converted values.
      • If a value is true, it is converted to 1 and then compared. If a value is false, convert it to 0 and then compare.
      • If one value is an object and the other value is a number or string, the object is converted to the value of the original type, and then the comparison is buried. You can use the ToString () method of the object or the valueof () method to convert the object to a value of the original type. The inner class of the JavaScript core language typically attempts the valueof () method transformation before attempting the ToString () method transformation, but for the date class, the ToString () method is executed before the valueof () method is converted. Objects that are not part of the JavaScript core language can convert themselves to original values in a way that is defined by the JavaScript implementation.
      • The other combinations of values are not equal.
4)! Take counter
1 alert (! ""); // true
V. Nannan: (not a number) This is not a numeric data type: No. Note: NaN is not equal to any number and does not equate to its own
1 var a = "100px"; 2 var b = "100px"; 3 Alert (number (a)); // NaN 4 alert (number (b)); // NaN 5 Alert (number (a) ==number (b)); // false
Vi. Isnanisnan detects if a data is converted to a number, is Nan, returns true, otherwise false
1 var true ; 2 alert (IsNaN (a)); // false
There is a reliable and accurate way to detect Nan: Only Nan is its own, then we use the non-equal number (!). = =) to determine if a number equals itself, so that Nan can be detected
1 function Isreallynan (x) {2     return x!== x; 3 }

JavaScript data types and type conversions

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.