javascript--Data types

Source: Internet
Author: User

1. Type conversion
01) Other data types to Boolean type conversions
Convert by Boolean () function

"Boolean true false"
String non-empty Strings ""
Number any non 0 0 and Nan
Object NULL for any objects
Undefined N/a Undefined (n/a not applicable not applicable)
For example:
Boolean (""); False
Boolean (0); False
Boolean (NaN); False
Boolean (NULL)//false
Boolean (undefined)//false
Boolean ("Briup"); True
Boolean (1); True

02) Other data types to numeric type conversions
1.Number ()
1) If the converted value is Null,undefined,boolean,number
Number (true); 1
Number (false); 0
Number (NULL); 0
Number (undefined); NaN
Number (10); 10 If it is a numeric value, output as-is

2) If it is a string:
Number ("123"); If only numeric values are included, the corresponding values are converted
Number ("234.1");//parse to corresponding decimal
Number ("+12.1");//First sign bit, the rest is numeric, convert to corresponding value
Number ("1+2.3");//nan sign bit appears in other locations, resolves to NaN
Number ("0xa"); If only 16 binary formats are included, the corresponding decimal value is converted to
Number ("010"); Note "will not be parsed as an octal result of 10
Number (""); An empty string is converted to 0
Number ("123ac");//contains other characters: NaN
Number ("12"); 12
2.parseInt ()
1) If the converted value is Null,undefined,boolean,number
parseint (TRUE); NaN
parseint (FALSE); NaN
parseint (NULL); NaN
parseint (undefined);//nan
parseint (10); 10 If integer value, output as-is
parseint (10.3); 10 If it is a decimal, give up the content after the decimal point
2) If it is a string:
parseint ("123"); 123; If only numeric values are included, convert to corresponding values
parseint ("234.1");//234; the value after the decimal point is omitted
parseint ("+12.1");//12; The first is the sign bit, the remainder is the number, and the conversion to an integer
parseint ("0xa"); 10; If only 16 binary formats are included, the corresponding decimal values are converted to
parseint ("010"); 10; "Attention! "will not be parsed as an octal result of 10
parseint (""); Nan; empty string is converted to Nan
parseint ("1+2.3");//1; If the first is a numeric value, parsing backwards, and then finding a continuous number until the first non-numeric value is encountered, converting the previously acquired value to number returns
parseint ("123ac");//123;
3.parseFloat ()
Similar to parseint (), but also different:
1. The first decimal point in the string is valid, and the second decimal point is invalid after which the contents are omitted
2. Always ignore the leading 0
Parsefloat ("22.3.4")//22.3
Parsefloat ("022.34"); 22.34
3. Unable to parse 0x
03) Other data types to string type conversions
1. ToString () function
By default, ToString () is a string representation of a numeric value returned in decimal format, and by passing parameters you can enter a string value in binary, octal, hexadecimal, or even any valid binary format
var num = 10;
Num.tostring (); "10"
Num.tostring (2); "1010"
Num.tostring (8); "12"
Num.tostring (16); A
But null, undefined does not have the ToString () method
Null.tostring ()//Error typeerror:null has no properties
Undefined.tostring (); Error typeerror:undefined has no properties
2. String () constructor
String (NULL) "NULL"
String (undefined); "Undefined"

2, operator
01) unary operator
+ +,--, The +,-operator returns a value type by applying any type of value, converting any of the values of any type to number and then doing the operation (through the number () method)
1. Increment + +
var a = "11";
1+a++;
1+ ++a;
Pre-increment or decrement operation before operation of the variable
Post: Increment or decrement after operation of the variable
Applies to operands of any data type, converts it to numbers (number ()) before use, and then operations, when applied to an object, first invokes the object's ValueOf method to get an actionable value if the subclass object overrides only ToString (). Call the method.
2. Descending--
Pre-increment or decrement operation before operation of the variable
Post: Increment or decrement after operation of the variable
Applies to operands of any data type, converts it to a number before use, and then applies the valueof method of the object to the object before it is applied, to obtain a value that can be manipulated, and if the subclass object overrides only ToString (), the method is called.
3. Plus +
Equivalent to calling number ();
var a = "12"
+a; 12 is equivalent to calling number ("12")
4. Minus-
When you apply a unary minus to a value, the value becomes negative.
When a unary minus is applied to a non-numeric value, it follows the same rule as the unary plus operator, and finally converts the resulting value to a negative number.

02) Boolean operator, non (not)
Logical Non!
The operator returns a Boolean value by applying any type of value. Convert any type of numeric value to Boolean, then reverse,
!a ==>! Boolean (a)
!0//true
! ""//true
! NaN//true
!false//true

With two times logical non, you can convert any data type to a Boolean type,!! A ==> Boolean (a)
! ""//false
03) Logic and && (with True, false or false) also known as short-circuit statements
can be applied to any numeric value. If there is an operand that is not a Boolean type, logical and does not necessarily return a Boolean type

1. If the first operand is a
null,nan,undefined,false,0, "" can be converted to a value of false when the value is returned
2. If the first number is other, return the second number
var S1 = 8;
var s2 = "Briup";
var s3 = "";
var result = S1 && s2; Briup
var result2 = s3 && S2; Empty string

04) Logic or | | (True, True, false)
False | |
If two operands are null,nan,undefined,false,0, "" can be converted to a value of false when the value is returned

If the first operand is null,nan,undefined,false,0, "" returns the second operand
05) Additive operator
1. Addition +
M + N
1) When M,n is not a string,object type, first convert the m,n to number type and then calculate
True + false; 1;number (True) +number (false);
True + 1; 2;number (true) + 1
null + undefined; Nan;number (undefined), NaN
2) When the M,n has a string, regardless of the other operand (but not the object), convert to String, and then stitch
"1" + true; 1true
"1" + undefined;//1undefined
"1" + 1; 11
3) When M,n has an object, if the object both overrides ToString and overrides the ValueOf method, first call the valueof method to get the return value, and the return value and the other operand are evaluated. If the object does not override the ValueOf method, the ToString method is called to get the return value, and the return value and the other operand are evaluated.
var o = {
Name: "Briup",
Valueof:function () {
return "1";
}
}
o+1; 2;o+1

2. Subtraction-
The return value is "numeric Type". Whether the operand is of any type, it is first converted to the number type using the numbers () converter, and then evaluated.
true-1; 0; 1-1
Null-true; -1; 0-1
1-undefined//nan
var o = {
Name: "Briup",
Valueof:function () {
return 1;
}
}
O-1; 0; 1-1

06) multiplicative operator
The return value is "Numeric Type" performs an automatic type conversion number when the operand is non-numeric ()
1. Multiplication *
If the two numbers are numeric, perform a regular multiplication calculation
If one of the operands is Nan, the result is Nan
Over value range return infinity
If there is an operand that is not a numeric value, the number () is called first to convert it to a numeric value.
2. Division/
If one of the operands is Nan, the result is Nan (0/0; NaN)
A non-0 value in addition to 0 Infinity
If there is an operand that is not a numeric value, the number () is called first to convert it to a numeric value.
var S1 = 5;
var s2 = 2;
var result = S1/s2; 2.5
3.% of residual
If the two numbers are numeric, perform a regular take-out calculation
If an operand is Nan, the result is Nan (any number%0; NaN)
If there is an operand that is not a numeric value, the number () is called first to convert it to a numeric value.

07) Relational operators
< > <= >=, returns a "Boolean" value
1. If the two operand is a string, compare the character encoding value of each character in the corresponding position in the string
"A" > "B"//false
"1" > "a"//false
2. If one operand is a number, the other operand is also converted to a numeric value for comparison
"3" >1; True
3>true; True 3>number (True)
3>undefined; False number (undefined) =nan; any and NaN comparison results are false
3. If an operand is an object, call ValueOf () before calling ToString () to compare the return value to another operand, or false if no override of ToString () valueOf ()
var o = {
Name: "Briup",
Valueof:function () {
return "13";
}
}
O&GT;2//true; ">2"
08) Equality operator, return "Boolean"
1. Equality and inequality = =,! = (first conversion in comparison)
1. If the two operands are all strings, compare the sequence of characters
2. If the two operands are numeric types, compare values for equality
3. If two operands are objects, a reference to the object being compared
4) NULL = = undefined//true
5) Nan is equal to any value (including Nan) and the result is false, and the unequal result is true.
6) If one operand is of type number and the other operand is one of undefined,null,boolean,string, convert these data types to numeric values before comparing

  2. Strict and non-uniform
    Compare no conversion only, compare the data types to which the two numbers belong, if the types are different, and then continue to compare the values of two numbers
    console.log ("55" = = 55); True
   console.log ("" = = =); False
   null = = Undifined;  //true Null derived from undifined
   null = = = Undifined;  //false

09) Three mesh operator? :
Variable = boolean_expression? True_value:false_value;
If Boolean_expression is true, assign true_value to variable, otherwise false_value will be assigned to variable

For example, to find the maximum value between any two numbers
function Max (m,n) {
Return m>n?m:n; If M>n is true returns m, if M>n is false, returns n
}
10) Assignment operator =
Assign the value on the right to the variable on the left
Can be used with other arithmetic operators *=/=%= + =
var a = 4;

A *= 3; + A = a*3;
11) Comma operator
You can perform multiple operations in one statement
var num1=1,num2 = 2, num3 = 3;

javascript--Data type

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.