2.9 Operator
2.9.1. Unary operator
The unary operator has only one parameter, which is the object or value to be operated on.
1. delete: Delete references to previously defined object properties or methods, but this operator cannot delete properties and methods that are not defined by the developer. Example:
var o=new Object;
o .name="Nicholas";
o.show=function(){
return "test";
};
console.log(o.name); //outpus Nicholas
console.log(o.show()); //outpus test
delete o.name;
delete o.show;
console.log(o.name); / /outpus undefined
console.log(o.show()); //outpus TypeError: o.show is not a function
delete o.toString;
console.log(o.toString ()); //outpus [object Object]
2. void: Returns undefined for any value. This operator is usually used to avoid outputting values that should not be output.
If you open a new window in a link, the code is as follows:
Click Me After clicking the link, [object] will appear in a new window. This is because the window.open() method returns a reference to the newly opened window. The object will then be converted into a string to be displayed. To avoid this result, you can call the window.open() function with the void operator:
Click Me 3. Pre-increment/pre-decrement operators: two operators borrowed from C. Example:
var iNum=10;
console. log( iNum); //outpus 11 same as iNum=iNum 1
console.log(iNum); //outpus 11
console.log(--iNum); //outpus 10 same as iNum=iNum -1
console.log(iNum); //outpus 10
4. Post-increment/post-decrement operators: two operators borrowed from C. Like pre-increment/pre-decrement, they also add or subtract 1 to a value. The difference is that postfix operators perform increment or decrement operations after calculating the expressions containing them. Example:
var iNum=10;
iNum- -
console.log(iNum); //outpus 9
console.log(iNum--); //outpus 9
console.log(iNum); //outpus 8
iNum
console.log(iNum); //outpus 9
console.log(iNum ); //outpus 9
console.log(iNum); //outpus 10
5. One-yuan addition and one-yuan subtraction: The usage is the same as that learned in high school mathematics. One-yuan addition has no effect on numbers, and one-yuan subtraction is to negate the numerical value. However, unary return and unary subtraction are similar to parseInt() when operating on strings. The main difference is that only for strings starting with "ox", the unary operator converts it into a decimal value. Example:
var iNum=25;
iNum= iNum;
console.log(iNum); //outpus 25
var sNum="50";
console.log(typeof sNum); //outpus string
console.log ( sNum); //outpus 50
console.log(typeof sNum); //outpus number
var sNum1="017";
var sNum2="0xB";
console .log(-sNum1); //outpus -17
console.log(-sNum2); //outpus -11
2.9.2 Bit operators
1. Bit operations NOT: Represented by (~), the processing process is as follows:
(1) Convert the operand to a 32-bit number
(2) Convert the binary form into its one’s complement;
(3) Convert binary complement to floating point number
Example:
var iNum1=25; //25 is equal to 0000 0000 0000 0000 0000 0000 0001 1001
var iNum2=~iNum1; //conver to 1111 1111 1111 1111 1111 1111 1110 0110
console.log(iNum2); //outpus -26
//The bit operator NOT essentially negates the number and then subtracts 1, so the following code will also work Get the same effect
var iNum3=25;
var iNum4=-iNum3-1;
console.log(iNum4);
2. Bitwise operation AND: by ( &) indicates that the binary form of the number is directly calculated. The rule is 1 only if all are 1, otherwise it is 0. Example:
var iNum1=25; //25 is equal to 0000 0000 0000 0000 0000 0000 0001 1001
var iNum2=iNum1&3; // 3 is equal to 0000 0000 0000 0000 0000 0000 0000 0011
console.log(iNum2); // and is 0000 0000 0000 0000 0000 0000 0000 0001 outpus 1
3. Bit operation OR: represented by (|), directly calculate the binary form of the number . The rule is 0 only if all are 0, otherwise it is 1. Example:
var iNum1=25; //25 is equal to 0000 0000 0000 0000 0000 0000 0001 1001
var iNum2=iNum1|3; // 3 is equal to 0000 0000 0000 0000 0000 0000 0000 0011
> 4. Bit operation XOR: by ( ^) means that the binary form of the number is calculated directly. The rule is that it is 1 only when a digit is stored in a 1, otherwise it is 0. Example:
var iNum1=25; //25 is equal to 0000 0000 0000 0000 0000 0000 0001 1001
var iNum2=iNum1^3; // 3 is equal to 0000 0000 0000 0000 0000 0000 0000 0011
console.log(iNum2); // xor is 0000 0000 0000 0000 0000 0000 0001 1010 outpus 26
5. Left shift operation: represented by (<<), move all the digits in the number to the left by the specified amount, retain the sign bit, and shift left One bit is equivalent to multiplying by 2.
6. Signed right shift operation: represented by (>>), moves all the digits in the number to the right by the specified amount, retaining the sign bit, and shifting one position to the right is equivalent to dividing by 2.
7. Unsigned right shift operation: represented by (>>>), moves all the digits in the number to the right by the specified amount. Positive numbers are treated exactly as signed right shifts, and negative numbers are treated as positive numbers.
Example:
var iOld=2;
var iOld1=64;
var iOld2=64;
var iOld3=-2;
var iNew=iOld<<5;
var iNew1=iOld1>>5;
var iNew2=iOld2>>>5;
var iNew3=iOld3>>>1;
console.log(iNew); //outpus 64
console.log(iNew1); / /outpus 2
console.log(iNew2); //outpus 2
console.log(iNew3); //outpus 2147483647
The calculation method of unsigned right shift of negative iOld3 is as follows :
First convert -2 into the unsigned equivalent form, that is, the two's complement of -2:
The non-negative version of -2 binary representation: 0000 0000 0000 0000 0000 0000 0000 0010
The binary The complement code: 1111 1111 1111 1111 1111 1111 1111 1101
Add 1 to the binary complement code: 1111 1111 1111 1111 1111 1111 1111 1110
Finally shift one bit to the right: 0111 111 1 1111 1111 1111 1111 1111 1111 i.e. is: 2147483647
2.9.3 Boolean operator
1. Logical NOT: represented by (!), the return value must be a Boolean value, the behavior is as follows:
If the operand is an object, return false
If the operand is the number 0, return true
If the operand is any number other than 0, return false
If the operand is null, return true
If the operand is NaN, return true
If the operands are undefined, an error occurs
2. Logical AND: represented by (&&). If the operands are both of Boolean type, true will be returned only when the operands are both true, otherwise false will be returned. The operands of the AND operation can be of any type, and the return value is not necessarily a Boolean value:
If one operand is an object and the other is a Boolean value, return the object
If both operands are objects, return The second object
If an operand is null, return null
If an operand is NaN, return NaN
If an operand is undefined, an error occurs
Logic in ECMAScript AND is also a simple operation, that is, if the first operand determines the result, the second operand will not be calculated. Example:
var bFalse=false;
var bResult=bFalse&&bUnknow;
console.log(bResult); //outpus false
var bTrue=true ;
var bResult=bTrue&&bUnknow;
console.log(bResult); //outpus ReferenceError: bUnknow is not defined
3. Logical OR operator: represented by (||), if the operands are all Boolean, false will be returned only when the operands are false, otherwise true will be returned. The operands of the OR operation can be of any type, and the return value is not necessarily a Boolean value:
If one operand is an object and the other is a Boolean value, return the object
If both operands are objects, return The first object
If an operand is null, return null
If an operand is NaN, return NaN
If an operand is undefined, an error occurs
Logic in ECMAScript OR is also a simple operation, that is, if the first operand determines the result, the second operand will not be calculated. Example:
var bTrue=true;
var bResult=bTrue||bUnknow;
console.log(bResult); //outpus true
var bFalse=false;
var bResult=bFalse||bUnknow;
console.log(bResult); //outpus ReferenceError: bUnknow is not defined
2.9.4 Multiplicative operator
1. Multiplication operator: represented by (*). Under normal circumstances, it is the same as multiplication in mathematics. In special cases, there are some special values:
If the result of the operation is too large or too small, the generated result is Infinity or -Infinity
If an operand is NaN, the result is NaN
Infinity multiplied by 0, the result is NaN
Infinity multiplied by any number other than 0, the result is Infinity or -Infinity, determined by the second The sign of the operand determines
Infinity multiplied by Infinity, the result is Infinity
2. Division operator: represented by (/), under normal circumstances it is the same as multiplication in mathematics. In special cases there are some special values:
If the result of the operation is too large or too small, the result is Infinity or -Infinity
If an operand is NaN, the result is NaN
If Infinity is divided by Infinity, the result is NaN
Infinity is divided by any number Division, the result is Infinity
0 divided by a non-infinity number, the result is NaN
Infinity is divided by any number other than 0, the result is Infinity or -Infinity, determined by the sign of the second operand
3. Modulo operator: represented by (%). Under normal circumstances, it is the same as multiplication in mathematics. In special cases, there are some special values:
If the dividend is Infinity, or the divisor is 0, the result is NaN
Infinity is divided by Infinity, the result is NaN
If the divisor is an infinite number, the result is the dividend
If the dividend is 0, the result is 0