The JavaScript operator

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

First, what is an expression???

is a phrase in ecmscript, the interpreter can be computed to turn it into a value, the simplest expression is a literal or variable name, a single literal and a combination literal is collectively referred to as an expression.

Two or one-tuple operator

1. Delete operator: Deletes a reference to a previously defined object property or method. For example:
var o=new Object;
O.name= "Superman";
alert (o.name); Output "Superman"
Delete O.name;
alert (o.name); Output "undefined"
The Name property is removed and set to undefined (that is, the value of the uninitialized variable that was created).Delete Cannot delete properties and methods that are not defined by the developer (that is, the ECMAScript definition).
For example, the following code will have an error: delete o.tostring ();
2, void operator: Returns undefined for any value. This operator is often used to avoid output values that should not be output.
For example: In the HTML page <a href= "Javascript:window.open (' About:blank ')" >click me</a>
Click the link, the link disappears in the webpage, display "[Object]" (ie, Firefox displays "[Object Window]", Google or the link). Because the window.open () method returns a reference to the newly opened window. The object is then converted to the string to be displayed. To avoid this result, you can call the window.open () function with the void operator:
<a href= "javascript:void (window.open (' About:blank '))" >click me</a>
This causes the window.open () call to return undefined, which is not a valid value and will not be displayed in the browser window.
3, the former increment/decrement operator: Same as in C. Operations occur before the expression is evaluated. such as: ++i; --I.
4, post increment/decrement operator: The postfix operator is an increment or decrement operation after the expression that contains them has been computed. such as: i++; I--。
5, unary addition and unary subtraction
Unary addition has no effect on numbers in nature: Var inum=25; Inum=+inum; alert (iNum); Output 25
The unary addition operator, when manipulating a string, computes a string in a way similar to parseint (), with the main difference being that a string that begins with "0x" (representing a hexadecimal number) can be converted to a decimal value by a unary operator. Therefore, using a unary addition to convert "010", the resulting is always 10, and "0xB" will be converted to 11.
var snum= "25"; Alert (typeof Snum); Output "string"
var inum=+snum; Alert (typeof INum); Output "Number"
Unary subtraction is negative for numeric values, similar to unary addition operators, and the unary subtraction operator converts a string to an approximate number, which in turn is negative.
var snum = "25"; Alert (typeof Snum); Output "string"
var iNum =-snum; alert (iNum); Output "-25"
Alert (typeof INum); Output "Number"

three, bitwise operators: A series of operators related to the binary, the actual operation does not seem to use, just skipped ...

The bitwise operator not is represented by a negative number (~).
The bitwise operator and is represented by the ampersand (&).
bitwise operator or by symbol (|) Said.
Bitwise operator XOR is represented by a symbol (^).
The left shift operation is represented by a two less than sign (<<).
The signed right shift operator is represented by a two greater than sign (>>).
The unsigned right shift is represented by a three greater than sign (>>>).
Three, Boolean operators
There are three Boolean operators, that is, not, and and OR.
1, logical not, with an exclamation point (!) Said. This operator is typically used to control loops. Unlike the logical OR and logical and operators, the logical NOT operator must return a Boolean value. The logical NOT operator behaves as follows:
Returns False if the operand is an object.
Returns true if the operand is the number 0.
Returns False if the operand is any number that is 0 unexpectedly.
Returns true if the operand is null.
Returns true if the operand is Nan.
If the operand is undefined, an error occurs.
Example: var B = true;
while (!b) {...}
2. The logical AND operator, denoted by the double ampersand (&&). There is only one case when the result is true: true && true = true;
The operands of a logical AND operation can be of any type, not just a Boolean value. If an operand is not the original Boolean value, the logical AND operation does not necessarily return a Boolean value:
If one operand is an object and the other is a Boolean value, the object is returned.
If the two operands are objects, the second object is returned.
Returns null if one of the operands is null.
If an operand is Nan, a nan is returned.
If an operand is undefined, an error occurs.
If the first operand is false, the result cannot be true regardless of the value of the second operand.
3, logical OR operator, with double vertical bar (| |) Said. There is only one case where the result is false: false | | False=false; All other conditions are true.
Similar to the logical AND operator, a logical OR operation does not necessarily return a Boolean value if an operand is not a Boolean value:
If one operand is an object and the other is a Boolean value, the object is returned.
If two operands are objects, the first object is returned.
Returns null if one of the operands is null.
If an operand is Nan, a nan is returned.
If an operand is undefined, an error occurs.
A logical OR is also a simple operation, and for a logical OR operator, the second operand is no longer computed if the first arithmetic value is true.
Iv. multiplicative operators
1, multiplication operator, denoted by an asterisk (*), used to multiply two numbers. However, when dealing with special values, the multiplication in ECMAScript has some special behaviors:
If the operands are numbers, perform the normal multiplication, and if the result is too large or too small, the result is Infinity or-infinity.
If an operand is Nan, the result is Nan.
Infinity times 0 and the result is Nan.
Infinity is multiplied by any number other than 0, and the result is Infinity or-infinity, which is determined by the symbol of the second operand.
2, the division operator, with a slash (/), with the second number in addition to the first number. For special values, the special behavior is as follows:
If the operands are numbers, perform a regular trigger operation, and if the result is too large or too small, the resulting result is Infinity or-infinity.
If an operand is Nan, the result is Nan.
0 except for a non-infinite number, the result is Nan.
Infinity is removed by any number other than 0, and the result is Infinity or-infinity, which is determined by the symbol of the second operand.
3. Modulo operator, denoted by percent sign (%). If the operands are numbers, perform a general arithmetic division operation, returning the remainder. Special behavior:
If the divisor is Infinity, or the divisor is 0, the result is Nan.
If the divisor is an infinite number, the result is dividend.
If the divisor is 0, the result is 0.
Five, additive operators
1, addition operator (+), special behavior:
One of the operands is Nan, and the result is Nan.
Infinity plus-infinity, the result is Nan.
If an operand is a string, the following rules apply:
If the two operands are strings, concatenate the second string to the first string.
If only one operand is a string, the other operand is converted to a string, and the result is a string of two strings concatenated into it.
2, subtraction operator (-), if the two operands are numbers, arithmetic subtraction will be performed. Special rules:
If the operand is Nan, the result is Nan.
An operand is not a number, and the result is Nan.
Vi. relational Operators
The relational operator is less than, greater than, less than or equal to, and greater than or equal to the two-number comparison operation, as compared to the arithmetic comparison operation. Each relational operator returns a Boolean value.
For a string, the code for each character in the first string is numerically compared to the code for the character at the corresponding position in the second string.
1, the code of uppercase letters is less than lowercase code, so to get the results according to the real alphabetical order, you must convert two operands to the same case, and then compare.
2. When comparing numbers in the form of two strings, their character codes are compared.
3. Compare a string and a number, ECMAScript will convert the string to a number, and then compare them in numerical order. If the string cannot be converted to a number, then a Nan is returned, and any relational operation that contains Nan will return false. Therefore, this returns false.
Seven, equal sex operators
1, equal sign (= =), and non-equal sign (! =) to determine whether the two operands are equal, both operators perform type conversions. The basic rules for performing type conversions are as follows:
If one of the operands is a Boolean value, convert it to a numeric value before checking for equality. False converts to 0,true to 1.
If one operand is a string and the other is a number, try converting the string to a number before checking for equality.
If one operand is an object and the other is a string, try to convert the object to a string (call the ToString () method) before checking for the other sex.
If one operand is an object and the other is a number, try to convert the object to a number before checking for the other sex.
The operator also adheres to the following rules when making comparisons:
The value null and undefined are equal.
When checking for equality, null and undefined cannot be converted to other values.
If an operand is Nan, the equal sign returns false, and the non-equal sign returns TRUE. Even though the two operands are Nan, the equals sign still returns false because Nan is not equal to Nan, according to the rule.
If the two operands are objects, then their reference values are compared. If two operands point to the same object, then the equals sign returns TRUE, otherwise two operands are not equal.

  
2, full equals and non-full equals
equals and non-equal operators are all equal and non-full equals. The two operators do the same as equals and non-equals, except that they do not perform type conversions until they are checked for equality.
The equals sign is represented by three equals (= = =) and returns true only if the type conversion operand is not required to be equal.
var snum= "55"; var inum=55; alert (snum==inum); Output "true"
alert (snum===inum); Output "false"
The non-full equals sign is represented by two exclamation points plus two equal signs (!==), which returns true only if no type conversion operand is required.
var snum= "55"; var inum=55; Alert (snum! = iNum); Output "false"
Alert (Snum!== iNum); Output "true"
Viii. Other operators
1, the conditional operator, that is, the ternary operator: variable = boolean_expression? True_value:false_value;
2. Assignment operator (=) Compound assignment operator: + =,-=, *=,/=,%=, <<=, >>=, >>>=
3. The comma operator allows you to perform multiple operations in a single statement with the comma operator. such as: Var inum1=1,inum2=2;

The JavaScript operator

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.