Common operators in JavaScript _ javascript skills

Source: Internet
Author: User
Tags bitwise operators
Summary of commonly used operators in JavaScript. For more information, see. I. unary Operators
1. delete OPERATOR: delete references to previously defined object attributes or methods. For example:
Var o = new Object;
O. name = "superman ";
Alert (o. name); // output "superman"
Delete o. name;
Alert (o. name); // output "undefined"
The name attribute is deleted and set to undefined (that is, the value of the created uninitialized variable ). Delete cannot delete attributes and Methods undefined by the developer (that is, defined by ECMAScript.
For example, the following code will produce an error: delete o. toString ();
2. void OPERATOR: returns undefined for any value. This operator is usually used to avoid output values that should not be output.
For example, Click Me on an HTML page
Click this link. The link on the webpage disappears and "[object]" is displayed. (in IE, "[object Window]" is displayed in Firefox, and this link is still displayed in Google ). 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 use the void operator to call the window. open () function:
Click Me
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. Increment/Decrement Operator: The operator is used in the same way as in C. The operation occurs before the calculation expression. For example, ++ I; -- I.
4. Post-increment/post-increment operators: suffix operators are incremental or decrement operations only after the expressions that contain them are calculated. For example, I ++; I --.
5. One-dimensional addition and one-dimensional Subtraction
The one-dimensional addition essentially has no effect on numbers: var iNum = 25; iNum = + iNum; alert (iNum); // output 25
When the unary addition operator operates on a string, it calculates the string in a similar way as parseInt (). The main difference is that it is a string starting with "0x" (representing a hexadecimal number ), the unary operator can convert it to a decimal value. Therefore, if you convert "010" by adding a dollar, the result is always 10, and "0xB" is converted to 11.
Var sNum = "25"; alert (typeof sNum); // output "string"
Var iNum = + sNum; alert (typeof iNum); // output "number"
One-dimensional Subtraction is used to calculate the negative value. It is similar to the one-dimensional addition operator. The one-dimensional subtraction operator also converts the string to an approximate number and calculates the negative value.
Var sNum = "25"; alert (typeof sNum); // output "string"
Var iNum =-sNum; alert (iNum); // output "-25"
Alert (typeof iNum); // output "number"
Ii. bitwise operators: A series of Operators related to binary, which seem to be not very useful in actual operations, just skipped ......
The bitwise operator NOT is composed of a negative number (~) .
The AND operator is represented by the Ampersand.
The bitwise operator OR is represented by the symbol (|.
The bitwise operator XOR is represented by the symbol (^.
The Left shift operation is represented by two smaller signs (<).
The shifted right operator is represented by two greater signs (>.
The unsigned right shift is represented by three greater than signs (>>>.
Iii. Boolean operators
There are three types of Boolean operators: NOT, AND, and or.
1. Logic NOT, with an exclamation point (!) . This operator is usually used to control loops. Unlike the logical or and logical AND operators, the logical NOT operator must return a Boolean value. The action of the logical NOT operator is as follows:
If the number of operations is an object, false is returned.
Returns true if the number of operations is 0.
If the number of operations is 0, the return value is false.
Returns true if the number of operations is null.
Returns true if the number of operations is NaN.
An error occurs if the number of operations is undefined.
For example, var B = true;
While (! B ){...... }
2. The logical AND operator is expressed by a pair AND an ampersand. When the result is true, there is only one situation: true & true = true;
The number of logical AND operations can be any type, not just Boolean values. If the number of operations is not the original Boolean value, the logical AND operation does not necessarily return the Boolean value:
If one operation is an object and the other is a Boolean value, this object is returned.
If both operations are objects, the second object is returned.
If the number of operations is null, null is returned.
If the number of operations is NaN, NaN is returned.
An error occurs if the number of operations is undefined.
If the first operation is false, no matter what the value of the second operation is, The result cannot be true.
3. Logical OR operator, represented by a double vertical line (|. If the result is false, only one of the following is possible: false | false = false; otherwise, all are true.
Similar to the logical AND operator, if the number of operations is not a Boolean value, the logical OR operation does not necessarily return a Boolean value:
If one operation is an object and the other is a Boolean value, this object is returned.
If both operations are objects, the first object is returned.
If the number of operations is null, null is returned.
If the number of operations is NaN, NaN is returned.
An error occurs if the number of operations is undefined.
Logical OR is also a simple operation. For logical OR operators, if the first operation value is true, the second operation is no longer calculated.
Iv. Multiplication Operators
1. Multiplication operator, represented by an asterisk (*), used to multiply two numbers. However, when processing special values, multiplication in ECMAScript has some special behaviors:
If the number of operations is a number, perform a regular Multiplication operation. If the result is too large or too small, the generated result is Infinity or-Infinity.
If the number of operations is NaN, the result is NaN.
Infinity is multiplied by 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 number of operations.
2. Division operator. It is expressed by a slash (/) and the second number is used to divide the first number. For special values, the special behavior is as follows:
If the number of operations is a number, a general trigger operation is executed. If the result is too large or too small, the generated result is Infinity or-Infinity.
If the number of operations is NaN, the result is NaN.
0 is a non-infinite number, and the result is NaN.
Infinity is divided by any number other than 0 and the result is Infinity or-Infinity, which is determined by the symbol of the second number of operations.
3. modulo operator, expressed by percent (%. If the number of operations is a number, the remainder is returned after a general arithmetic division operation. 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 the divisor.
If the divisor is 0, the result is 0.
5. Addition Operators
1. Addition operator (+), special behavior:
The number of operations is NaN and the result is NaN.
Infinity plus-Infinity, and the result is NaN.
If the number of operations is a string, use the following rules:
If both operations are strings, connect the second string to the first string.
If only one operation number is a string, convert the other operation number to a string and the result is a string connected by two strings.
2. subtraction operator (-). If both calculation numbers are numbers, subtraction is performed. Special rules:
If the number of operations is NaN, the result is NaN.
The result of an operation is NaN, instead of a number.
Vi. Relational operators
The Relational operators smaller than, greater than, less than or equal to, and greater than or equal to the two numbers are compared. The comparison method is the same as the arithmetic comparison operation. Each relational operator returns a Boolean value.
For a string, the code of each character in the first string is compared with the code of the character at the corresponding position in the second string.
1. The Code of uppercase letters is smaller than that of lowercase letters. Therefore, to obtain the true alphabetic comparison result, you must convert the two arithmetic numbers into the same case and compare them.
2. When comparing two numbers in string form, compare their character code.
3. When comparing a string and a number, ECMAScript converts the string to a number and then compares them in numerical order. If the string cannot be converted to a number, NaN is returned, and false is returned for any relational operation containing NaN. Therefore, false is returned.
VII. Equality Operators
1. equal sign (=) and non-equal sign (! =). To determine whether the two operators are equal, both operators perform type conversion. The basic rules for executing type conversion are as follows:
If a number is a Boolean value, convert it to a numeric value before checking equality. Convert False to 0, and true to 1.
If one operation is a string and the other is a number, convert the string to a number before checking for equality.
If one operation is an object and the other is a string, you must convert the object to a string (call the toString () method) before checking the equality ).
If one operation number is an object and the other is a number, try to convert the object to a number before checking the equality.
During comparison, this operator also complies with the following rules:
The null and undefined values are equal.
When equality is checked, null and undefined cannot be converted into other values.
If the number of operations is NaN, the equal sign returns false, and the non-equal sign returns true. Even if both operations are NaN, the equal sign returns false because according to the rule, NaN is not equal to NaN.
If both operations are objects, their reference values are compared. If the number of two operations points to the same object, the equal sign returns true; otherwise, the number of two operations varies.


2. Full and non-full numbers
Similar operators of equal signs and non-equal signs are full and non-full. The two operators are the same as equal signs and non-equal signs, but they do not perform type conversion before checking equality.
The full equal sign is represented by three equal signs (=). true is returned only when the number of type conversion operations is equal.
Var sNum = "55"; var iNum = 55; alert (sNum = iNum); // output "true"
Alert (sNum === iNum); // output "false"
A non-equal sign is composed of two exclamation points and two equal signs (! =) Indicates that true is returned only when the number of type conversion operations is not equal.
Var sNum = "55"; var iNum = 55; alert (sNum! = INum); // output "false"
Alert (sNum! = INum); // output "true"
8. Other operators
1. Conditional operator, I .e., ternary OPERATOR: variable = boolean_expression? True_value: false_value;
2. Value assignment operator (=) Compound value assignment operator: + =,-=, * =,/=, % =, <=, >=, >>>=
3. the comma operator can be used to execute multiple operations in one statement. For example, 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.