Common operators in JavaScript summary _javascript tips

Source: Internet
Author: User
Tags arithmetic bitwise first string numeric value
One or one-dollar 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 deleted 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 typically used to avoid values that the output should not output.
For example: In an HTML page <a href= "Javascript:window.open (' About:blank ')" >click me</a>
Click on the link, the link in the Web page disappears, display "[Object]" (ie, Firefox shows "[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 display. To avoid this result, you can invoke 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 appear in the browser window.
3, pre-increment/forward decrement operator: same as in C. An operation occurs 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 is evaluated. such as: i++; I--。
5, one-element addition and one-element subtraction
In essence, unary addition has no effect on numbers: Var inum=25; Inum=+inum; alert (inum); Output 25
The unary addition operator, when manipulating a string, computes the string in the same way as parseint (), with the main difference being that a string that begins with "0x" (a hexadecimal number) can be converted to a decimal value by a unary operator. Therefore, the conversion of "010" with one-element addition 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"
A unary subtraction is a negation of a numeric value, similar to a unary addition operator, which converts a string to an approximate number and, in addition, a negative value.
var snum = "25"; Alert (typeof Snum); Output "string"
var inum =-snum; alert (inum); Output "-25"
Alert (typeof Inum); Output "Number"
bitwise operator: A series of operators related to binary, the actual operation does not seem to use, skipped ...
The bitwise operator is not 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 exclamation point (!) Said. This operator is typically used to control loops. Unlike a logical OR and a logical AND operator, the logical NOT operator returns a Boolean value. The logical NOT operator behaves as follows:
Returns False if the OP count is an object.
Returns True if the OP count is the number 0.
If the OP count is 0 unexpected any number, return false.
Returns true if the shipping count is null.
Returns true if the shipping count is Nan.
If the OP count is undefined, there is an error.
Example: var B = true;
while (!b) {...}
2, the logical AND operator, represented by a double ampersand (&&). There is only one condition when the result is true: true && true = true;
The operands of a logical AND operation can be of any type, not more than a Boolean value. If an operand is not of the original Boolean value, the logical AND operation does not necessarily return a Boolean value:
If one of the operands 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 a shipment count is null.
If a shipping count is Nan, return nan.
If a shipment count is undefined, an error occurs.
If the first count is false, then the result cannot be true, regardless of the value of the second operand.
3, logical OR operator, with double vertical bars (| |) Said. The result is only one case of false: false | | False=false; All other cases are true.
Similar to the logical AND operator, a logical OR operation does not necessarily return a Boolean value if one of the operands is not a Boolean:
If one of the operands is an object and the other is a Boolean value, the object is returned.
If the two operands are objects, the first object is returned.
Returns null if a shipment count is null.
If a shipping count is Nan, return nan.
If a shipment count is undefined, an error occurs.
Logical OR is also a simple operation, and for the logical OR operator, the second operand is no longer computed if the first operation value is true.
multiplicative operator
1, multiplication operator, with an asterisk (*), used to multiply two numbers. However, when dealing with special values, the multiplication in ECMAScript also has some special behaviors:
If the operands are numeric, perform a regular multiplication operation, and if the result is too large or too small, then the resulting result is Infinity or-infinity.
If a shipping count is Nan, the result is Nan.
Infinity times 0, and the result is Nan.
Infinity times any number other than 0, the result is Infinity or-infinity, determined by the symbol of the second operand.
2, 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 numeric, the normal trigger operation is performed, and if the result is too large or too small, the resulting result is Infinity or-infinity.
If a shipping count is Nan, the result is Nan.
0 The result is Nan, except for a number that is not infinitely large.
Infinity by any number other than 0, the result is Infinity or-infinity, determined by the symbol of the second operand.
3, modulo operator, expressed in percent (%). If the operands are numeric, perform regular arithmetic division operations and return the remainder. Special behavior:
If the divisor is Infinity, or the divisor is 0, the result is Nan.
If the divisor is infinitely large, the result is dividend.
If the divisor is 0, the result is 0.
Five, additive operator
1, addition operator (+), special behavior:
A shipping count is Nan, and the result is Nan.
Infinity plus-infinity, the result is Nan.
If a shipment count is a string, the following rules apply:
If the two operands are strings, concatenate the second string onto the first string.
If only one operand is a string, the other is converted to a string, and the result is a string of two strings concatenated.
2, subtraction operator (-), if two operands are numbers, arithmetic subtraction will be performed. Special rules:
If the shipping count is Nan, the result is Nan.
An operand is not a number, and the result is Nan.
Six, relational operators
The relational operator is less than, greater than, less than or equal to, and greater than or equal to two number of comparison operations, the comparison is the same as the arithmetic comparison operation. Each relational operator returns a Boolean value.
For strings, 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, uppercase code is less than the lowercase letter code, so to get the true alphabetical order to compare the results, you must convert the two arithmetic to the same case, and then compare.
2. When comparing numbers in two strings, they are compared to their character codes.
3. Comparing a string and a number, ECMAScript converts strings to numbers and compares them in numerical order. If the string cannot be converted to a number, then the Nan is returned, and any relational operations that contain Nan are returned false. Therefore, this returns false.
vii. equal-sex operators
1, equal sign (= =) and non-equal sign (!=), to determine whether two operands are equal, both operators will be type conversion. The basic rules for performing type conversions are as follows:
If an operand is a Boolean value, convert it to a numeric value before checking for equality. False converts into 0,true converted to 1.
If one of the operands 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 sex.
If one operand is an object, and the other is a number, try to convert the object to a number before checking for sex.
When you compare, the operator also adheres to the following rules:
Value null and undefined equal.
You cannot convert null and undefined to other values when checking for equality.
If one of the operands is Nan, the equal sign returns false, and a non-equal sign returns TRUE. Even if the two operands are Nan, the equals sign returns false because, according to the rule, Nan is not equal to Nan.
If two operands are objects, then the reference value is compared. If two operands point to the same object, the equal sign returns TRUE, otherwise the two operands are unequal.


2, full equals and non-full equals
equals and non-equals are all equals and not equals. The two operators do the same as equals and non equals, except that they do not perform type conversions until the equality is checked. The
full sign is represented by three equal signs (= =) and returns true only if the type conversion shipping count is equal.
var snum= "two"; var inum=55; alert (snum==inum);/output "true"
Alert (snum===inum);/output "false"
non-full equals by two equals sign ( !==) indicates that true is returned only if no type conversion operands are equal.
var snum= ";" Var inum=55; alert (snum!= inum);/output "false"
Alert (snum!==);/output "true"
eight, other operations Character
1, the conditional operator, or ternary operator: variable = boolean_expression? true_value:false_value;
2, assignment operators (=) composite Assignment operators: + =, =, *=,/=,%=, <<=, >>=, >>>=
3, comma operators you can perform multiple operations in one statement with a comma operator. such as: Var inum1=1,inum2=2;

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.