JavaScript Quick Start (5)-expression operations, javascript Quick Start
Value assignment is in the form of Left = right. If multiple value assignment operations exist in the same expression, the value is calculated from right to left. For example:
A = B = c; // and the following two rows are equivalent to B = c; a = B;
Another form of value assignment is the compound value assignment operator, which is the left value op = the right value. op = indicates the combination of some operators and =, a op = B is equivalent to a op B. For example, the following two statements are equivalent:
a += b;a = a + b;
Op can be one of the following operators: +,-, *,/, %, <, >>>>, &, |, ^
The element of the numeric operation is a numerical value. The operator is a op B and the target type is also a numerical value. For example:
var a = 3 + 5; // a == 8
In particular, ++, -- represents increment and decrease respectively. The form is a op or op a, and the effect of a ++ is equivalent to a = a + 1. For example:
var a = 0;a++; // a = 1
Note that ++ a and a ++ are different in complex operations. ++ A indicates that a is incremented before the updated a value is involved in the operation, and a ++ indicates that the original a value is involved in the operation before increasing. For example:
var a = 1;var b = 1 + (a++); // b == 2console.log(a); // 2b = 1 + (++a); // b == 4console.log(a); // 3
Of course, if only incremental statements exist in a statement, they do not need to be distinguished.
var a = 1;a++; // a == 2++a; // a == 3
Bitwise operators and object types are numerical values. The operators are ~, &, |, ^, <,>, And so on. Note that bitwise operations are performed on binary numbers, that is, the values are first converted to binary, and the results are also binary. Of course, we can see that it has been converted to a decimal number, if not specified. Starting from the integer of ECMAScript
ECMAScript integers are of two types: signed integers (positive and negative values are allowed) and unsigned integers (only positive numbers are allowed ). In ECMAScript, All integers are signed integers by default. What does this mean?
A signed integer uses 31 bits to represent the value of an integer, 32nd bits to represent an integer, 0 represents a positive number, and 1 represents a negative number. The value range is-2147483648 to 2147483647.
You can store signed integers in binary format in two different ways. One is used to store positive numbers and the other is used to store negative numbers. Positive numbers are stored in real binary format. Each bit in the first 31 represents the power of 2, starting from 1st bits (bit 0), indicating 2.0, 2nd bits (bit 1) represent 21. The useless bits are filled with 0, which is ignored. For example, the expression of number 18 is displayed.
The binary version of 18 only uses the first five digits, which are the valid digits of this number. Convert the number into a binary string to see the valid bit:
Var iNum = 18; alert (iNum. toString (2); // output "10010"
This Code only outputs "10010", not the 32-bit expression of 18. Other digits are not important, because only the first five digits can be used to determine the decimal value. As shown in:
Negative numbers are also stored as binary code, but the format is binary complement. You can perform the following three steps to calculate the binary complement:
- Determine the binary representation of the non-negative version of the number (for example, to calculate the binary complement of-18, first determine the binary representation of 18)
- Returns the binary anticode, that is, to replace 0 with 1 and 1 with 0.
- Add 1 to the binary anti-code
To determine the binary representation of-18, you must first obtain the binary representation of 18, as shown below:
0000 0000 0000 0000 0000 0000 0001 0010
Next, calculate the binary anticode as follows:
1111 1111 1111 1111 1111 1111 1110 1101
Finally, add 1 to the binary anti-code, as shown below:
1111 1111 1111 1111 1111 1111 1110 1101 1---------------------------------------1111 1111 1111 1111 1111 1111 1110 1110
Therefore, the binary representation of-18 is 1111 1111 1111 1111 1111 1111 1110 1110. Remember, when processing signed integers, developers cannot access 31 characters.
Interestingly, after converting a negative integer into a binary string, ECMAScript is not displayed in the form of a binary complement, but is output in the form of a negative sign before the standard binary code of the absolute value of the number. For example:
Var iNum =-18; alert (iNum. toString (2); // output "-10010"
This code outputs "-10010" instead of a binary complement code, which is used to avoid access to bit 31. For ease, ECMAScript processes integers in a simple way, so that developers do not have to care about their usage.
On the other hand, the unsigned integer processes the last digit as another digit. In this mode, the 32nd BITs do not represent the digit, but the value is 2.31. Because of this extra bit, the value range of the unsigned integer is 0 to 4294967295. For integers smaller than 2147483647, the unsigned integer seems to be the same as the signed integer, while an integer greater than 2147483647 uses 31 (in a signed integer, this is always 0 ).
After an unsigned integer is converted to a string, only the valid bits are returned.
Note:All integers are stored as signed integers by default. Only the bitwise operators of ECMAScript can create unsigned integers.
NOT operation (~)
NOT calculated by a negative number (~) It is a unary operator (only one operator), in the form ~ A.
Bit operations are NOT three steps:
- Converts an operation number to a 32-bit number.
- Converts a binary number to its binary anticode.
- Converts a binary number to a floating point number.
For example:
Var iNum1 = 25; // 25 equals 00000000000000000000000000011001var iNum2 = ~iNum1;// Convert it to 11111111111111111111111111100110 alert (iNum2); // output "-26"
The bitwise operation NOT is to calculate the negative value for the number and then subtract 1, so 25 is changed to 26. The following method can also be used to obtain the same method:
Var iNum1 = 25; var iNum2 =-iNum1-1; alert (iNum2); // output-26
AND (&)
AND operations AND are represented by AND signs. It alignment the digits in each number, AND then use the following rules to perform the AND operation on the two digits in the same position (if AND only if the two digits are 1, 1 is returned ):
| Number in the first digit |
Number in the second digit |
Result |
| 1 |
1 |
1 |
| 1 |
0 |
0 |
| 0 |
1 |
0 |
| 0 |
0 |
0 |
For example, to perform AND operations on Numbers 25 AND 3, the Code is as follows:
Var iResult = 25 & 3; alert (iResult); // output "1"
The result of the AND operation on 25 AND 3 is 1. Why? The analysis is as follows:
25 = 0000 0000 0000 0000 0000 0000 0001 1001 3 = 0000 0000 0000 0000 0000 0000 0000 0011---------------------------------------------AND = 0000 0000 0000 0000 0000 0000 0000 0001
It can be seen that in 25 and 3, only one digit (bit 0) Stores 1, so other digits generate 0, so the result is 1.
OR (|)
OR is expressed by the symbol (|. When calculating each digit, the OR operator follows the following rules (0 is returned only when both digits are 0 ):
| Number in the first digit |
Number in the second digit |
Result |
| 1 |
1 |
1 |
| 1 |
0 |
1 |
| 0 |
1 |
1 |
| 0 |
0 |
0 |
Use the AND operator as an example to perform OR operations on 25 AND 3. The Code is as follows:
Var iResult = 25 | 3; alert (iResult); // output "27"
The result of performing the OR operation on 25 and 3 is 27:
25 = 0000 0000 0000 0000 0000 0000 0001 1001 3 = 0000 0000 0000 0000 0000 0000 0000 0011--------------------------------------------OR = 0000 0000 0000 0000 0000 0000 0001 1011
It can be seen that a total of four digits in two numbers are 1, and these digits are passed to the result. The binary code 11011 equals 27.
XOR (^)
The XOR operation is represented by a symbol (^. XOR is different from OR. When only one digit stores 1, it returns 1. The truth table is as follows:
| Number in the first digit |
Number in the second digit |
Result |
| 1 |
1 |
0 |
| 1 |
0 |
1 |
| 0 |
1 |
1 |
| 0 |
0 |
0 |
Perform XOR operations on 25 and 3. The Code is as follows:
Var iResult = 25 ^ 3; alert (iResult); // output "26"
The result of performing XOR operations on 25 and 3 is 26:
25 = 0000 0000 0000 0000 0000 0000 0001 1001 3 = 0000 0000 0000 0000 0000 0000 0000 0011---------------------------------------------XOR = 0000 0000 0000 0000 0000 0000 0001 1010
It can be seen that a total of four digits in two numbers are 1, and these digits are passed to the result. The binary code 11010 equals 26.
Left shift operation (<)
The Left shift operation is represented by two smaller signs (<). It moves all digits in a number to the left. For example, if the number 2 (equal to 10 in binary) is shifted to 5 digits, the result is 64 (equal to 1000000 in binary ):
Var iOld = 2; // equals to binary 10var iNew = iOld <5; // equals to binary 1000000 decimal 64
Note:Five more spaces are available on the right of the number when the number is shifted to the left. The Left shift operation fills these spaces with 0, making the result a complete 32-bit number.
Note:The Left shift operation retains the sign bit of the number. For example, if you move-2 to the left to 5 digits, the result is-64 instead of 64. "Is the symbol still stored in the 32nd-bit format ?" Yes, but this is done in the ECMAScript background. Developers cannot directly access 32nd digits. Even if a negative value in the form of a binary string is output, it is displayed as a negative number (for example,-2 will display-10 .)
Right Shift operation (>)
Bitwise operation
The shifted right operator is represented by two greater signs (> ). It shifts all the digits in the 32-bit number to the right and retains the symbol (positive or negative) of the number ). The bitwise shift operator is the opposite of the bitwise shift operator. For example, to shift 64 to the right, change to 2:
Var iOld = 64; // equals to binary bytes 00var iNew = iOld> 5; // equals to binary 10 decimal 2
Similarly, after the number is moved, the space is vacant. This time, the blank space is located on the left side of the number, but behind the symbol bit. ECMAScript fills these spaces with the symbol bit values and creates a complete number, as shown in:
Unsigned right shift operation
The unsigned right shift operator is represented by three greater than signs (>>>). It shifts all digits of the unsigned 32-digit number to the right. For positive numbers, the result of the unsigned right shift operation is the same as that of the signed right shift operation.
In the example of a signed right shift operation, change 64 to 5 bits to 2:
Var iOld = 64; // equals to binary bytes 00var iNew = iOld >>> 5; // equals to binary 10 decimal 2
For negative numbers, the situation is different.
The unsigned right shift operation fills all spaces with 0. For positive numbers, this is the same as the operation for the signed right shift operation, while the negative number is processed as a positive number.
Because the result of the unsigned right shift operation is a 32-bit positive number, the unsigned right shift operation of negative numbers always produces a very large number. For example, if you shift-64 to the right by 5 digits, 134217726 is returned. How can we get this result?
To achieve this, you need to convert the number into an equivalent form without a symbol (although the number itself is still signed). You can obtain this form using the following code:
var iUnsigned64 = -64 >>> 0;
Then, use the toString () of the Number type to obtain its true bit representation. The base is 2:
alert(iUnsigned64.toString(2));
This will generate 11111111111111111111111111000000, that is, the binary complement representation of the signed integer-64, but it is equal to the unsigned integer 4294967232.
For this reason, be careful when using the unsigned right shift operator.
There are two types of logical operations: one is changing the target data type, and the other is not. A typical example of the former is a non-logical operation. No matter what type of operation element, after a non-logical operation is executed, it is converted to a bool value. For example:
var a = "123";var b = !a; // false
The other form does not change the target type and supports Boolean short-circuit operations, including "logical and" (&) and "logical or" (|) operations. The so-called Boolean short circuit refers to determining the operation result when determining the first operation element, without the need to process the second operation element. The specific operation rules are as follows:
- When the first operation element is true, the logic or operation returns the first operation element (note that the type is not changed), and the logic and operation returns the second operation element.
- When the first operation element is false, the logic or operation returns the second operation element, and the logic and operation returns the first operation element.
Here are some special examples:
function and(a, b) { return a && b;}function or(a, b) { return a || b;}var a = and("str", false); // a === falsevar b = or("str", false); // b === "str"var c = and(0, "str2"); // c === 0var d = or(0, "str2"); // d === "str2"The most common use of this operation is to process real parameters. We know that the real parameter list and the form parameter list in JavaScript can be of varying length. How can we deal with the unequal part? For example:
function add(a, b) { return a + b;}var a = add(5); // NaNThe result is NaN, which is obviously not the result we want. We prefer to return 5. We can use logical operations:
function add(a, b) { b = b || 0; return a + b;}var a = add(5); // 5var b = add(5, 2); // 7If the length of the parameter is greater than the length of the real parameter, the excess part is undefined. B = B | 0 can assign 0 to B when B is undefined (Boolean value is false), so that B is still a numerical value and involved in the operation. Of course, this process is still very simple. For example, when the input B is a non-null string, this function will still fail, so the correct method should be to determine whether it is a numerical value type, however, it is a good way to perform logical operations without the exception of API users.
In string operation JavaScript, there is only one string operation, that is, string connection. Of course, strings can also be involved in other operations, such as comparison, equivalence, value assignment, and so on, but we will put them in other categories for discussion. The built-in methods in the string type, such as cutting strings, do not belong to the expression operation described here. The operator number corresponding to the String concatenation operation is "+ ". Note that "+" is a multi-dimensional symbol. It can be used for addition of numeric values, unary operators, and string connections. The specific operation is determined by the number and type of elements. If there are two or more operators and one of the first two operators is a string, other non-strings are converted to strings, and the target type is strings. For example:
var a = "str" + "ing"; // a === "string"var b = "str" + 0; // b === "str0"var c = 0 + "str"; // c === "0str"var d = "str" + undefined; // d === "strundefined"var e = null + "str" + undefined; // e === "nullstrundefined"var f = null + undefined + "str"; // f == NaN
The purpose of the comparison calculation equivalence check is to determine whether two variables are the same or equal. The same and different operators refer to the operators "=" and "! = ", Indicating equal and not equal to the operator" = "and"! =. We can use a table to represent:
Equality detection in comparison operations
| Name |
Operator |
Description |
| Equal |
= |
Compare two expressions to see if they are equal |
| Not Supported |
! = |
Compare two expressions to see if they are not equal |
| Strictly equal |
=== |
Compare two expressions to see if the values are equal and have the same data type |
| Not strictly equal |
! = |
Compare two expressions to see if they have different values or data types. |
Equality calculation rules in equality Detection
Equality calculation rules in equality Detection
| Type |
Operation Rules |
| Compare two value types |
Convert to values of the same data type for data equivalent comparison |
| Comparison between value types and reference types |
Convert the data of the reference type to the value type, and then compare the data equivalent. |
| Comparison of Two reference types |
Compare referenced addresses |
If you are vague about the value type and reference type, you can see the previous article about JavaScript variables. Same operation rule in equality Detection
Same operation rule in equality Detection
| Type |
Operation Rules |
| Compare two value types |
Different data types are bound to be different. Perform numerical equivalent comparison when data types are the same |
| Comparison between value types and reference types |
Must be different |
| Comparison of Two reference types |
Compare referenced addresses |
We can see that there is no difference between the two reference types, which are the same and equal. Let's look at an example:
var str1 = new String("str");var str2 = new String("str");console.log(str1 == str2); // falseconsole.log(str1 === str2); // falseThe concept of sequence Detection is very difficult to understand. In other words, it is actually relatively large. There are four comparison operators:>, >=, <, and <=. The meaning is not to be understood by everyone. Three data types are available for sequence Detection: boolean, string, and number. Their sequence values are shown in the following table:
| Types of comparable Sequences |
Sequence Value |
| Boolean |
0 ~ 1 |
| String |
0 ~ 255(* Note 1) |
| Number |
NEGATIVE_INFINITY ~ POSITIVE_INFINITY(* Note 2) |
* Note 1: In general languages, character (char) data types are sorted by ASCII code. JavaScript does not contain any character type, but each character in a string is used as a single character for sequence Detection. * Note 2: negative infinity to positive infinity. In fact, there is a boundary, but this boundary has reached 10 ^ 308 (Number can be used. MAX_VALUE. There is no sequence value for the value NaN, and any value (including NaN itself) and NaN will be detected in sequence. The operation rules for the false sequence detection are as follows:
Sequence detection operation rules
| Type |
Operation Rules |
| Compare two value types |
Directly compare the data size in the sequence |
| Comparison between value types and reference types |
Convert the data of the reference type to the value type, and then compare the data size in the sequence |
| Comparison of Two reference types |
Meaningless, always returns false |
This part of function call is described in detail in the JavaScript function in the previous section. The operator "()" can only be used after a function or a variable pointing to a function. Otherwise, an exception is triggered.
Special operators have some operators in JavaScript, which are used to affect the operation effect instead of directly generating the operation effect. The operation objects of these operators are usually expressions ", rather than the value of an expression ". In addition, some operators do not directly perform variable value operations, but instead perform variable operations. Detailed operators and their functions are as follows:
| Target |
Operator |
Function |
Remarks |
| Operation Element |
Typeof |
Returns a string of the data type. |
|
| Operation Element |
Instanceof |
Returns an inheritance relationship. |
|
| Operation Element |
In |
Returns a member relationship. |
|
| Operation Element |
Delete |
Delete a member |
|
| Expression |
Void |
Avoid expression return values |
Make the expression always return undefined |
| Expression |
? : |
Execute one of the two expressions by condition |
It is also called a three-object conditional operator. |
| Expression |
() |
Grouping expressions and adjusting operation order |
Also known as priority Operators |
| Expression |
, |
Expressions are executed sequentially. |
Also known as the multiple evaluate or comma Operator |
Computing priority plays an important role in complex operations. For example, we can look at an expression:
void 1+2
We know that void is used to make the expression always return undefined. So, is the void operation object 1 or 1 + 2? In other words, which of the following has a higher priority for void and +? Assume that if the void has a higher priority, the number of void operations will be 1, and the operation result will be (void 1) + 2, that is, undefined + 2, the result is NaN. On the contrary, if + has a higher priority, the result will be void (1 + 2), that is, void 3, that is, undefined. So what is it? The fact is NaN, that is, void has a higher priority. We will detail common operators in JavaScript and their precedence. Note that the table has a lower priority from top to bottom.
Priority of operators in JavaScript
| Priority |
Operator |
Description |
| Highest |
. [] () |
Object member access array subscript function call |
| Slave |
++ -~ ~ Delete new typeof void |
Unary operator |
| Upper |
*/% |
Modulo of multiplication and division |
| To |
+-+ |
Addition and subtraction string connections |
| Lower |
<>>>> |
Shift |
| Of |
<<=>> = Instanceof |
Sequence Detection inheritance relationship |
| Excellent |
=! ===! = |
Equality Detection |
| First |
& |
Bitwise AND |
| Level |
^ |
Bitwise OR |
| Yes |
| |
By bit or |
| Yue |
&& |
Logic and |
| Come |
| |
Logic or |
| Yue |
? : |
Ternary Condition |
| Low |
= Op = |
Value assignment |
| Lowest |
, |
Multiple evaluate |
When different or identical operators with the same priority appear in a statement at the same time, they are executed from left to right. Of course, like in CSS! Important and JavaScript also have a symbol that forcibly changes the priority, that is, (). The operation in parentheses takes precedence over the operation outside brackets.