JavaScript Advanced Programming (eight): basic concept-Operator

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators

Operators include: arithmetic operators, bitwise operators, relational operators, and equality operators.

Unary operator

1, can only operate a worthy operator, that is, increment and decrement operators;

2, Increment (+ +) and decrement (-) operators include: Pre-and post-type. The predecessor is before the variable you want to manipulate, and the post type is behind the variable you want to manipulate.

3. When you perform a pre-increment and decrement operation, the value of the variable is changed before the statement is evaluated, and the post-increment-decrement operation executes after the statement that contains them executes. eg

var age=; // forward Decrement var ageother=--age+2;   // returns 23 (decrements before executing the statement) // decrease in post-position var 2;  // returns 24 (the statement is executed first, then the decrement is performed)

4, increment and decrement operations can also be applied to strings, booleans, and objects, but they are converted to numeric values before they are manipulated.

vars1='1';varS2='Z';varf='1.1';varb='true';varo={valueof:function () {return-1; }  }//perform an increment decrement operation++S1;//2++S2;//nan (does not contain a number, the converted value returns NaN)--f;//0.10000000000000009; (Error generated by floating-point type)b++;//2o++;//0 (object, call the ValueOf () method, and then convert to a numeric value to operate)

5, unary plus minus operator: put the plus sign (minus sign) in front of the value, there is no effect on the value "the same applies to strings, Booleans and objects, rules, such as increment and decrement operations." eg

var num=; num=+num;  //  - // But if it's a minus, it turns into the opposite number. var num2=; num2=-num2;  // -33

6, bitwise operators: All values in ECMAScript are stored in IEEE-754 64-bit format, but bitwise operators are not directly manipulating 64-bit values. Instead, the 64-bit value is converted to 32 bits, the operation is performed, and the last 64 bits are reversed.

7, bitwise operator operations include: bitwise NON (not ~), bitwise AND (&) "Calculation rules: the same as 1 to get 1", Bitwise OR (|) "Calculation rules: 1 is 1", bitwise XOR (^) "calculation rules: Different values to get 1".

8, Boolean operators: Logical non (not) logic and (and) logic or (or) 3 operations.

9, logical non (!) ) can be used with any data type and returns a Boolean value. Rules for logical non-operator adherence:

1) null, undefined, NaN, empty string, 0 will return true;

2) returns false for non-empty strings, objects, non-0 values (including Infinity Infinity)

  Summary : You can determine whether a variable exists by logical non.

10: Logic and (&&): two operands.  For example: Var rs=true && false; False

11, the logic and the operator applies to any data type, it follows the rule:

1) If the first operand is an object, a second operand is returned;

2) If the second operand is an object, the object is returned only if the first operator is true;

3) If two operators are objects, the second operand is returned;

4) If one of the operands is null, undefined, NaN, the values are returned.

12, Logic and (&&) operation: A short-circuit operation, that is, the first operand can determine the result, then the second operand is not evaluated. If the value of the first operand is false, then the second operation will no longer execute.

eg

var f=truevar rs= (f && s);  // catch to error. s for defining variable console.log (RS);    // This statement does not execute var f=false; var rs= (f && s); Console.log (RS);   //Return False,s will no longer execute

13, logical OR operator is similar to logic, the return value is not necessarily a Boolean value, following the rules:

1) If the first operand is an object, the first operand is returned;

2) If two operands are objects, the first operand is returned;

3) If the first operand evaluates to False, the second operand is returned

4) If one of the operands is null, undefined, NaN, the values are returned.

14, multiplicative operator: multiplication, division, modulo (remainder). For non-numeric operators, the number () is converted, i.e. non-numeric string = = "0, Boolean = =" 0 and 1.

15, the multiplicative operator special operation follows the rules:

1) If the product exceeds the range of Ecmaspcript, it will return Infinity or-infinity;

2) If one of the operands is Nan, the Nan is returned;

3) The infinity is multiplied by 0, then the Nan is returned;

4) Infinity and non-0 values, return infinity or-infinity, the symbol depends on non-0 value;

5) infinity is multiplied by infinity and returns infinity.

6) When the value is multiplied by a non-numeric value, the number () is called before the conversion, and then the following rules are followed.

16, division is similar to multiplication, different rules:

1) Infinity except infinity, return nan;

2) The non-0 value is divided by 0, then the Infinity or-infinity is returned, and the symbol depends on the symbol of the non-0 value;

3) If the value is not 0 divided by Infinity, then the Infinity or-infinity is returned, and the symbol depends on the symbol of 0 value;

17, modulus (remainder%): var rs=26% 5;

18, the processing of the special value of the mold, the following rules:

1) Infinity Infinity divided by a finite large value, returning Nan;

2) A finite large number divided by 0, returning Nan;

3) infinity divided by infinity, return nan;

4) 0 divided by 0 value (including infinity), return 0;

5) A finite large number divided by infinity, returning a finite large value; eg:

var rs= -Infinity; Console.log (rs);   // Ten var rs=  Infinity; Console.log (rs);   // Ten

19. Additive operators handle Special values:

1) Infinity plus-infinity, return nan;

2) +0 Plus-0, return +0;

3) If a value is added to a string, the concatenation of the string is obtained; eg:

//two strings, stitchingvarnus1='1';varNus2='2'; Console.log (Nus1+NUS2);//' a '//a value + a stringvarnum=1;vars='2'; Console.log (num+s);//' a '

20. The decrement operator deals with special values:

1) If the operator Nan is present, the Nan is returned;

2) infinity-infinity return nan;

3)-infinity minus-infinity, return nan;

4) Infinity minus-infinity, return to Infinity;

5)-infinity minus Infinity, return to-infinity;

6) Non-numeric subtraction, first call the number () conversion, and then perform the subtraction operation; eg:

varrs1=5-true;//4 (True is converted to 1)varrs2=nan-1;//nan (with Nan present)varrs3=5-3;//2varrs4=5-"';//5 ("0 after conversion")varrs5=5-NULL;//5 (Null converted to 0)varrs6=5-'2';//3

21. Relational operator: When the operand of the relational operator is a non-numeric value, data conversion or some strange operation is done. eg

1) If two operands are strings, the character encoding corresponding to two strings is compared; " two numeric string comparisons are the same" eg:

varrs="Bird"<'Apple';//true(Reason: The encoding of the character ' B ' is 66, the encoding of the character ' A ' is 97, so is less than)varRs2='Bird'. toLowerCase () <'Apple'. toLowerCase ();//false(Cause: When all is converted to lowercase, the character ' B ' is encoded at 98, the character ' a ' is encoded at 97, so is greater than)

Comparison of two numeric strings
var rs3= ' < ' 3 '; True (the character ' 2 ' is really less than ' 3 ')

Numeric and string
var rs4= ' <3; False (the character ' 23 ' is converted to a value of 23 before being compared, so it is greater than)

2) If the value is compared with a non-numeric value, it should be converted to a numerical value before being compared.

3) Any operands that are compared to Nan will return false.

22, equality operators: There are two cases: equality (= =) and inequality (! =): First converted to the same type in the comparison; congruent (= = =) and fully Unequal (!==): No conversion direct comparison "data types are also compared".

23. The following rules are followed for equality operations:

1) null and undefined are equal.

2) If two operands are objects, the comparison of two objects is not the same object. Returns true if the same object is pointed to, otherwise false.

3) None of the operands is equal to Nan. eg

NULL==undefined;//true
NaN!=nan; //truenan=='NaN';//false
Nan==nan; //false'5'==5;//true
True==1; //truefalse==0;//truefalse==1;//false

24, congruent and all unequal: data types are also equal; eg:

var rs= ('up'== =);  // False (different data types: string  values)var rs2= (= ==);  // true var rs3= (null===undefined);  // false (null and undefined data types are different)

25, conditional operator: syntax: var tt=boolean_expression? True_value:false_value; eg

var max= (Num1 >num2)? num1:num2;  // get the maximum value of two values

26. Assignment operator (=): The compound operator can be completed by adding the multiplicative operator and the additive operator before the equal sign. eg

var num=; Num=num+; // can be written in compound form: var num=; num+ =ten;

27, comma operator: A statement can perform multiple operations, mainly through the comma to complete.

1) If multiple variables are declared: Eg:var num=1, s= ' 3 ', flag=false;

2) can be used to assign values.  Eg:var num= (1,2,5,8,9,6,0); The value of NUM is 0 (because 0 is the last value of the expression, so the value of num is 0)

 

JavaScript Advanced Programming (eight): basic concept-operator

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.