JS Basic Concepts (3)

Source: Internet
Author: User
Tags bitwise bitwise operators modulus

"5" operator

(1) Unary operators: operators that can manipulate only one value
    • Increment, decrement operator

++a--a Front-facing
a++ a--Rear-facing
(These four operators apply to any value and cannot be converted to a number to Nan)

    • Unary Plus, unary minus operator (indicates positive, negative)
(2) Bitwise operators: Manipulating values by the bits in memory that represent values
    • All the values in ECMAScript are stored in the IEEE-754 64-bit format, but the bitwise operator does not directly manipulate the 64-bit value, but instead converts the 64-bit to a 32-bit integer, then executes the operation and turns the result to 64 bits.
    • For signed integers, the first 31 bits in the 32-bit are used to represent the value of an integer. The 32nd bit is the symbol (symbol bit) that represents the numeric value: 0 for positive numbers and 1 for negative numbers.
    • The value of the symbol bit determines the format of the other bit values:

Positive number: in pure binary storage
Negative number: Binary complement
Step: 1 "to find the absolute value of the binary code;
2 "To find the binary of the anti-code, will be 0 to replace 1, 1 is replaced by 0;"
3 "Get the binary Anti-code plus 1.

// 18 binary code          0000 0000 0000 0000 0000 0000 0001 0010// negation code 1111                  1111 1111 1111 1111 1111 1110 1101// Anti-code plus 1                                                     1//———————————————————————————— ————————————————————————————————————————————————//                    1111 1111 1111 1111 1111 1111 1110 1110
    • When you apply a bit operation to a special Nan and infinity value, both values are treated as a limit.
    • Applying a bitwise operator to a non-numeric value first converts using the number () function.

1 "Bitwise NON (~): The result of performing a bitwise non is the inverse code of the returned value
2 "Bitwise AND (&): Bitwise AND operation only in the corresponding bit of the two values are 1 to return 1, any one is 0, the result is 0
3 "Bitwise OR (|): Bitwise OR operation only in the corresponding bit of the two values is 0 to return 0, any one is 1, the result is 1
4 "Bitwise XOR" (^): Bitwise XOR OR operation the corresponding bit in both values is 1 or 0 returns 0, only one is 1, only returns 1
5 shift Left (<<):: The left shift operator will move all digits of the value to the left of the specified number of digits, shifted to the left, the right side of the original value of the extra empty space will be filled with zeros; left shift does not affect the symbol bit of the operand
6 Signed right Shift (>>): Moves the value to the right, but retains the sign bit, after the right shift, the left side of the original value will be filled with the value of the sign bit 7 "Unsigned Right Shift (>>>): For positive, signed right shift and unsigned right shift results are the same, for negative numbers , unsigned right shifts fill vacancies with zeros, and negative binary codes are treated as positive binary codes

(3) Boolean operator

1 "Logical Non-(!)

    • ! Object//false
    • ! Empty string//true
    • ! Non-empty string//false
    • ! Value 0//true
    • ! Any non-0 value (including infinity)//false
    • ! Null//true
    • ! NaN//true
    • ! Undefined//true

Simulates the behavior of a Boolean () transformation function using two logical non-operators.
2 "Logic and (&&)
Logic and operations can be applied to operands of any type, not just Boolean values. In the case where an operand is not a Boolean value, the logic and operation do not necessarily return a Boolean value:

    • Returns the second operand if the first operand is an object;
    • If the second operand is an object, the object is returned only if the value of the first operand evaluates to true;
    • If the two operands are objects, the second operand is returned;
    • Returns null if one of the operands is null;
    • If one of the operands is Nan, a nan is returned;
    • If one operand is undefined, the undefined is returned;

The logic and operation is a short-circuit operation, that is, if the first operand can determine the result, then the second operand is no longer evaluated (the first operand is false).
3 "Logic or (| |)
A logical OR operation can be applied to any type of operand, not just a Boolean value. In the case where an operand is not a Boolean value, the logical OR operation does not necessarily return a Boolean value:

    • If the first operand is an object, the first operand is returned;
    • If the evaluation result of the first operand is false, the second object is returned;
    • If the two operands are objects, the first operand is returned;
    • Returns NULL if all two operands are null;
    • If the two operands are Nan, a nan is returned;
    • If the two operands are undefined, the undefined is returned;

The logic or operation is also a short-circuit operation, that is, if the first operand can determine the result, then the second operand is no longer evaluated (the first operand is true).

(4) Multiply sex operator

1 "Multiplication (*)

    • If the operands are numeric, perform a regular multiplication calculation, that is, the result of multiplying two positive or two negative numbers is positive, and if only one operand is signed, the result is a negative number. If the product exceeds the representation range of the ECMAScript value, the Infinity or-infinity is returned;
    • If one of the operands is Nan, the result is Nan;
    • If the infinity is multiplied by 0, the result is Nan;
    • If the Infinity is multiplied by a value other than 0, the result is Infinity or-infinity, depending on the symbol of the signed operand;
    • If the infinity is multiplied with infinity, the result is infinity;
    • If there is a number that is not a numeric value, it is called in the background to convert it to a numeric value before applying the rule above.

2 "Division (/)

    • If the operands are numeric, perform a regular division calculation, that is, the result of dividing two positive or two negative numbers is a positive number, and if only one operand is signed, the result is negative. Returns Infinity or-infinity if the quotient exceeds the representation of the ECMAScript value;
    • If one of the operands is Nan, the result is Nan;
    • If 0 is removed by 0, then the result is Nan;
    • If a finite number of non-0 is removed by 0, the result is Infinity or-infinity, depending on the symbol of the signed operand;
    • If the infinity is infinity, the result is Nan;
    • If Infinity is removed by any non-0 value, the result is Infinity or-infinity, depending on the symbol of the signed operand;
    • If there is a number that is not a numeric value, it is called in the background to convert it to a numeric value before applying the rule above.

3 "modulus, remainder (%)

    • If the operands are numeric, perform a general division calculation and return the remainder;
    • If the divisor is an infinity value and the divisor is a finite number, the result is Nan;
    • If the divisor is a finite large number and the divisor is 0, the result is Nan;
    • If the infinity is infinity, the result is Nan;
    • If the divisor is a finite number and the divisor is an infinite number, the result is a divisor;
    • If the divisor is 0, the result is 0;
    • If there is a number that is not a numeric value, it is called in the background to convert it to a numeric value before applying the rule above.
(5) Additive operator

1 "addition (+)

    • If the two operators are numeric, perform a regular addition calculation, and then return the results according to the following rules:
    • If one of the operands is Nan, the result is Nan;
    • If it is infinity plus infinity, then the result is infinity;
    • If it is-infinity plus-infinity, then the result is-infinity;
    • If it is Infinity plus-infinity, the result is Nan;
    • If it is +0 plus +0, the result is +0;
    • If it is-0 plus-0, then the result is-0;
    • If it is +0 plus-0, the result is +0.
    • If an operator is a string, then the following rules apply:
    • If the two operands are strings, the second operand is stitched together with the first operand;
    • If only one operand is a string, the other operand is converted to a string, and then the two strings are stitched together.
    • If an operand is an object, numeric, or Boolean value, call their ToString () method to obtain the corresponding string value, and then apply the string's rules. For null and undefined, call the string () function and get the string "null" and "undefined", respectively.

2 "Subtraction (-)

    • If the two operators are numeric values, perform a regular arithmetic subtraction operation and return the result:
    • If one of the operands is Nan, the result is Nan;
    • If it is infinity minus infinity, the result is Nan;
    • If it is-infinity minus-infinity, the result is Nan;
    • If it is Infinity minus-infinity, the result is Infinity;
    • If it is-infinity minus Infinity, the result is-infinity;
    • If +0 minus +0, the result is +0;
    • If it is-0 minus-0, the result is +0;
    • If it is +0 minus-0, the result is-0;
    • If one of the operators is a character, a Boolean value, a null, or a undefined. The number () function is called in the background to convert it to a numeric value, and then the subtraction calculation is performed based on the preceding rule. If the result of the conversion is Nan, the result of the subtraction is Nan;
    • If an operand is an object, the valueof () method of the calling object obtains the numeric value that represents the object. If the resulting value is Nan, the result of the subtraction is Nan. If the object does not have a valueof () method, the ToString () method is called and the resulting string is converted to a numeric value.
(6) Relational operators

The operators that are less than (<), greater than (>), less than or equal (<=), greater than or equal (>=) return a Boolean value

    • If the two operands are numeric, a numeric comparison is performed.
    • If all two operands are strings, compare the character encoding values of the two strings.
    • If one operand is a numeric value, the other operand is converted to a number and then a numeric comparison is performed.
    • If an operand is an object, the valueof () method of the object is called, and the results are compared according to the previous rule. If the object does not have a valueof () method, the ToString () method is called, and the resulting results are compared against the previous rule.
    • If an operand is a Boolean, it is converted to a numeric value before the comparison is performed.
    • Any operands that are related to Nan are compared, and the result is false.
(7) Equality operator

1 Equals and not equal (= = and! =)--Convert first and then compare
Before converting the operands to compare equality, equality and inequality follow the following rules when converting different data types:

    • If one operand is a Boolean, convert it to a numeric value before comparing equality---False to 0,true to 1;
    • If one operand is a string and the other operand is a number, the string is converted to a numeric value before equality is compared;
    • If one operand is an object and the other is not, then the valueof () method of the object is called, and the resulting base type value is compared with the preceding rule;
    • null and undefined are equal;
    • Null and undefined cannot be converted to any other value until equality is compared;
    • If one of the operators is Nan, the equality operator returns FALSE, and the inequality operator returns true, even if the two operands are Nan, because, according to the rules, Nan is not equal to Nan;
    • If the two operands are objects, then the comparison is not the same object. The equality operator returns TRUE if all two operands point to the same object; otherwise, it returns false;
An expression Value
Null = = undefined True
"Nan" = = Nan False
5 = = NaN False
Nan = = Nan False
Nan! = Nan True
5 = = "5" True
false = = 0 True
true = = 1 True
true = = 2 Flase
undefined = = 0 False
Null = = 0 False

2 "congruent and non-congruent ()--only compare and not convert
The two operands return true in the case of non-conversion, not equal to return false;

null; // true null; // false
(8) Conditional operator

Variable = boolean_expression? True_value:false_value;

(9) Assignment operator

Simple Assignment (=) assigns the value on the right to the variable on the left
Multiply/Assign value (*=)
Divide/Assign Value (/=)
Modulus/Assignment (%=)
Add/Assign value (+ =)
Subtract/Assign value (-=)
Shift Left/Assignment (<<=)
Signed Right Shift/Assignment (>>=)
Unsigned Right Shift/assignment (>>>=)

(10) Comma operator

The comma operator is used to declare multiple variables;

var num1 = 0, num2 = 1, num3 = 2;

In addition, the comma operator can also be used to assign a value, and the comma operator always returns the last item in the expression.

var // num value is 5

JS Basic Concepts (3)

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.