JavaScript operator Summary

Source: Internet
Author: User
Tags arithmetic operators bitwise bitwise operators first string logical operators

Operator

JavaScript has assignments, comparisons, arithmetic, bits, logic, strings, and special operators. This chapter describes the operators and some information about operator precedence.

Table 2.1 A concise list of all the JavaScript operators.

Table 2.1 JavaScript Operators

operator Classification operator Description
Count
Postoperative
Exercise
For
Character
+ (addition) adds two numbers.
++ (self-increment) adds a variable that represents a numeric value (you can return a new or old value).
- (inverse number, subtraction) returns the inverse of the argument as the inverse number operation. Subtract two numbers as a binary operator.
-- (self-subtraction) Subtract one variable that represents a numeric value (you can return a new or old value).
* (multiplication) Multiplies two numbers.
/ (division) divides two numbers.
% (redundancy) The remainder of dividing two numbers.
String operator + (string addition) to concatenate two strings.
+= Connects two strings and assigns the result to the first string.
Luo
Series
Exercise
For
Character
&& (Logical AND) returns TRUE if the two operands are true. Otherwise, false is returned.
|| (logical OR) returns False if the two operands are false. otherwise returns true.
! (logical non) returns FALSE if its single operand is true. otherwise returns true.
-bit
Exercise
For
Character
& (bitwise AND) if the two operand corresponds to a bit of 1, then the bit returns 1.
^ (Bitwise XOR) If the two operand corresponds to a bit with only one 1, then the bit returns 1.
| (bitwise OR) if the two operand corresponds to a bit of 0, then the bit returns 0.
~ (negation) reverses each bit of the operand.
<< (left) shifts each bit in the binary form of the first operand to the left, and the number shifted is specified by the second operand. The right seat is 0.
>> (arithmetic right shift) shifts each bit in the binary form of the first operand to the right, and the number shifted is specified by the second operand. Ignores the bits that are moved out.
>>> (logical right SHIFT) shifts each bit in the binary form of the first operand to the right, and the number of shifts is specified by the second operand. Ignores the shifted bits, and the left slot is 0.
Fu
Value
Exercise
For
Character
= Assigns the value of the second operand to the first operand.
+= Adds two numbers, and assigns the sum to the first number.
-= Subtracts two numbers and assigns the difference to the first number.
*= Multiplies two numbers and assigns a product to the first number.
/= Divides two numbers and assigns a quotient to the first number.
%= Calculates the remainder of dividing two numbers and assigns the remainder to the first number.
&= Performs a bitwise-and assigns the result to the first operand.
^= Performs a bitwise XOR, and assigns the result to the first operand.
|= Executes a bitwise OR, and assigns the result to the first operand.
<<= Performs a left shift and assigns the result to the first operand.
>>= Performs an arithmetic right shift and assigns the result to the first operand.
>>>= Performs a logical right SHIFT and assigns the result to the first operand.
Than
More
Exercise
For
Character
== Returns true if the operands are equal.
!= Returns true if the operands are not equal.
> Returns true if the left operand is greater than the right operand.
>= Returns true if the left operand is greater than or equal to the right operand.
< Returns true if the left operand is small less right operand the operand.
<= Returns true if the left operand is less than or equal to the right operand.
Special
Josh
Exercise
For
Character
?: Executes a simple "if...else" statement.
, Evaluates two expressions, returning the value of the second expression.
Delete Allows you to delete a property of an object or an element specified in an array.
New Allows you to create an instance of a user-defined object type or built-in object type.
This A keyword that can be used to reference the current object.
typeof Returns a string that indicates the type of the operand that was not evaluated.
void The operator specifies that an expression is to be evaluated but does not return a value.

Assignment operator

The assignment operator assigns a value to the left operand based on the value of its right operand side.

Implementation version Navigator 2.0

The most basic assignment operand is an equal sign (=), which assigns the value of the right operand directly to the left operand. In other words, x = y assigns the value of Y to X. The other assignment operators are the abbreviated form of the standard operation, listed in Table 2.2.

Table 2.2 Assignment operators

abbreviation operator meaning
x + = y x = x + y
X-= y x = XY
X *= y x = x * y
X/= y × = x/y
X%= y x = x% y
X <<= y x = x << y
X >>= y x = x >> y
X >>>= y x = x >>> y
X &= y x = x & y
X ^= y x = x ^ y
X |= y x = x | Y

Comparison operators

The so-called comparison operator compares the operands on either side, and returns a logical value based on the result of the comparison as true or false. Operands can be numeric or string values. If you use string values, the comparison is based on the standard dictionary order.

Implementation version Navigator 2.0

The relevant content is listed in table 2.3. For the example in this table, we assume that var1 is given a value of 3, and var2 is given a value of 4.

Table 2.3 Comparison Operators

operator Description back to the real example
Equal (= =) Returns true if the operands are equal. 3 = = Var1
Unequal (! =) Returns true if the operands are unequal. Var1! = 4
Greater than (>) Returns true if the left operand is greater than the right operand. Var2 > Var1
Greater than or equal to (>=) Returns true if the left operand is greater than or equal to the right operand. Var2 >= var1
VAR1 >= 3
Less Than (<) Returns true if the left operand is small less right operand the operand. Var1 < VAR2
Less than or equal to (<=) Returns true if the left operand is less than or equal to the right operand. Var1 <= var2
VAR2 <= 5

Arithmetic operators

gives the given number (constant or variable) a given calculation and returns a numeric value. The standard arithmetic operators are add (+), subtract (-), multiply (*), and (/) arithmetic. These operators work in the same way as in other programming languages.

Implementation version Navigator 2.0

% (take surplus)

The use of the remainder operator is as follows:

Var1% VAR2

The take remainder operator returns the first operand divided by the remainder of the second operand. For the above example, the VAR1 variable is returned divided by the remainder of the VAR2 variable. A more specific example is that 12 of 5 will return 2.

+ + (self-increment)

The self-increment operator uses the following:

var++ or ++var

The increment operator will increment the operand (self plus 1) and return a value. If it is written behind a variable (such as x + +), the value before the increment is returned. If written in front of a variable (such as ++x), the increment value is returned.

For example, if X is 3, then the statement y = x + + will first set Y to 3 and then increment by 4. Instead, the statement y = ++x will first increment the X to 4, and then the Y to 4.

--(self-reduction)

The use of the decrement operator is as follows:

var--or--var

The increment operator will decrement the operand (minus 1 itself) and return a value. If written behind a variable (such as x--), the value before the decrement is returned. If written in front of a variable (such as--x), the self-minus value is returned.

For example, if X is 3, then the statement y = x--will first set Y to 3 and then the X from 2. Instead, the statement y =--x will first subtract the X from 2 and then the Y to 2.

-(for inverse number)

The inverse number of operands will get the opposite number of operands. For example, y =-X assigns the value of the X-inverse number to Y, which means that if X is 3, Y will get 3 and X or 3.

Bitwise operators

When performing bit operations, operators treat operands as a string of bits (1 and 0) instead of decimal, hexadecimal, or octal digits. For example, the decimal 9 is the binary 1001. Bitwise operators operate in binary form at execution time, but the returned value is still the standard JavaScript value.

Table 2.4 JavaScript Bit operators overview

Table 2.4-bit operators

operator usage Description
Bitwise-AND A & B If the two operand corresponds to a bit of 1, then the bit returns 1.
Bitwise OR A | B If the two operand corresponds to a bit of 0, then the bit returns 0.
Bitwise XOR OR a ^ b If the two operand corresponds to a bit with only one 1, then the bit returns 1.
Seek counter } B Reverses each bit of the operand.
Move left A << b Shifts the binary form of a to the left B-bit. The right seat is 0.
Arithmetic Right Shift A >> b Move the binary form of a to the right B bit. Ignores the bits that are moved out.
Logical Right Shift A >>> b Move the binary form of a to the right B bit. Ignores the moved bits, and the left side is 0.

Bitwise logical Operators

Implementation version Navigator 2.0

In principle, the workflow of a bitwise logical operator is this:

    • Converts the operand to an integer value of 32 bits and represents it in binary notation.

    • Each of the first operands is paired with the corresponding bit of the second operand: first to first, second to second, and so on.

    • Apply the operator to each pair of bits, and the end result is a bitwise combination.

For example, a binary representation of 9 for 1001,15 is represented as 1111. So if you apply a bitwise logical operator to these two numbers, the result should look like this:

    • & 9 Results for 9 (1111 & 1001 = 1001)

    • 15 | 9 is 15 (1111 | 1001 = 1111)

    • 15 ^ 9 for 6 (1111 ^ 1001 = 0110)

Shift operator

Implementation version Navigator 2.0

The shift operator requires two operands: the first is the value to be shifted, and the second specifies the number to shift to the first number. The direction of the shift is determined by the operator used.

The shift operator converts two operators to a 32-bit integer value and returns the same result as the left operand type.

<< (shift left)

This operator shifts the first operand to the left by a few bits. The moved bits are ignored. Right vacancy 0.

For example, the 9<<2 result is 36 because 1001 shifts two bits to 100100, which is 36.

>> (arithmetic right shift)

This operator will move the first operand to a number of bits. The moved bits are ignored. The left slot is the same value as the original leftmost position.

For example, the 9>>2 result is 2 because 1001 shifts right two bits to 10, which is 2. Conversely, the -9>>2 result is-3, because the sign bit is taken into account.

>>> (logical right Shift)

This operator will move the first operand to a number of bits. The moved bits are ignored. The left margin is 0.

For example, the 19>>>2 result is 4 because 10011 shifts right two bits to 100, which is 4. For non-negative numbers, the arithmetic right shift and the logical right shift result are the same.

logical operators

The logical operator uses a Boolean value (Boolean logical value) as the operand and returns a Boolean value.

Implementation version Navigator 2.0

Table 2.5 describes the very clear.

Table 2.5 logical operators

operator usage Description
and (&&) Expr1 && EXPR2 Returns if Expr1 is False, otherwise returns EXPR2.
or (| |) Expr1 | | Expr2 Returns EXPR2 if the Expr1 is true.
Non-(!) !expr Returns False if expr is true, otherwise true.

Example

Consider the following script:

<script language= "JavaScript1.2" > "
V1 = "Cat";
V2 = "Dog";
V3 = false;
Document.writeln ("T && t return" + (v1 && v2));
Document.writeln ("F && t return" + (v3 && v1));
Document.writeln ("T && F return" + (v1 && v3));
Document.writeln ("F && F return" + (v3 && (3 = = 4))); Document.writeln ("T | | T returns "+ (V1 | | v2));
Document.writeln ("f | | T returns "+ (V3 | | v1));
Document.writeln ("T | | F return "+ (V1 | | V3));
Document.writeln ("f | | F return "+ (V3 | | (3 = = 4)));
Document.writeln ("!t return" + (!V1));
Document.writeln ("!f return" + (!V3));
</script>

The script will display the following:

T && t return dog
F && T returns false
T && F return False
F && F return False
T | | T back to the cat
f | | T back to the cat
T | | F Back to the cat
f | | F return False
!t return False
!f returns True

JavaScript operator Summary

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.