Summary of JavaScript common operators and operators
Category |
Operator |
Arithmetic operators |
+, –, *,/,% (modulo) |
String operator |
+ String Connection + = string Join compound |
Boolean operator |
! , &&, | | |
Unary operator |
+ +,--、 + (one yuan Plus),-(one yuan minus) |
Relational comparison operators |
<, <=, >, >=,! =, = =, = =,!== |
Bitwise operators |
~ Bitwise non-& Bitwise AND | Bitwise OR ^ bitwise XOR << left shift >> signed right shift >>> unsigned right shift |
Assignment operator |
=, compound assignment (+ =,-=, *=,%=) Composite bitwise Assignment (~=, &=, |=, ^=, <<=, >>=, >>>=) |
Object operators |
. property access, [] property or array access, new call constructor Common object, delete variable property deletion, void (return undefined), in judgment property, instanceof prototype judgment |
Other operators |
?: conditional operator, comma operator, () grouping operation, typeof type operator |
Precautions:
1. Arithmetic operators
1.1 In addition to the plus sign (+), if the operand is not of type number, it is automatically first called number () to convert it to type number before being evaluated.
1.2 Division sign (/) and modulo (%) do not differentiate between integers and floating-point numbers, and are automatically converted to floating-point numbers, such as 9/2 = 4.5 instead of 4,5.3% 3 = 2.3 instead of 2.
1.3 Any operation, as long as the operand contains Nan, the result is Nan. But it's not as if the result is Nan there must be an operand of Nan, for example 0/0 also returns Nan.
2. String operators
The 2.1 string connection symbol (+) is equivalent to the concat () function, which converts the operation data into a string and then connects. The numeric type is converted to a string when the + sign operation is performed in string and numeric form.
3. Boolean operators
3.1 && logic and is often applied to determine whether a variable or property has a definition, for example:
if (Object && object.name && object.name = ' name ') { // This will first determine the existence of an object, If it does not exist, it will not parse the Object.name thereby preventing the error from happening, as well as only object.name exist to compare this value. }
3.2 | | The case of logic or often applied to provide default values, and? The question mark operator is similar. For example
function= obj | | {}; // if the call to FN does not pass in obj, the obj is automatically assigned a value of undefined, and then because the corresponding Boolean value of undefined is false, // so an empty object {} is assigned to obj, and if the call is passed to obj, because the Boolean value of any object is true, // Therefore, you do not take the following {} to achieve the effect of a default value of obj {}. }
4. Unary operators
4.1 The pre-increment (minus) will first increase (minus) and then participate in other operations, then the other operations are then added (minus).
4.2 When a value that is not of type number is self-increment, it is implicitly converted to type # and then self-increment (minus).
5. Relational comparison operators
5.1 When the two sides are strings, the character encoding values are compared one after the other, as long as the larger person terminates the comparison and does not go backwards.
5.2 Comparison The two sides have a number type, then the non-type data will be converted to the value of type # and then compared.
5.3 When the operator is an object, call valueof () (if not, call ToString ()), and then compare the results.
5.4 Any number and Nan comparisons will return false.
6. Object Operators
6.1 by [] You can access the name is a variable or a property with a special character, and the name is a normal determined value when using the. dot number to access the object properties.
6.2 New calls the constructor to create an object that is pointed inside the constructor of this newly created object .
6.3 Delete, deleting a variable or property (a variable can be considered a global object or a property of the execution environment)
7. Other operators
7.1 typeof is an operator, not a function, that returns a string value (some differ slightly depending on the browser's extreme version)
type |
typeof Value |
type |
< P align= "Center" >typeof value |
|
typeof value |
undefined |
|
null |
|
boolean |
' Boolean ' |
|
|
string |
|
|
function ' |
typeof is generally used to determine the simple data type, if the object type, because most of the return is an object, the actual general will not be used;
And instanceof's judgment also needs to satisfy the condition of the same context, otherwise it will be wrong,
8. Common methods:
8.1 Use the unary Plus + direct implicitly to convert to number type. For example: Console.info (+true); 1, unary operator, converted to a value of 1
8.2 The use of an empty string can be directly implicitly converted to a string type. For example: Console.info ("+true"); True, implicitly converts to string ' true '
8.3 Use double logic non!! Implicitly converts to a Boolean type. For example: Var a= ' a '; Console.info (!! a); True two times inverse, implicitly converted to a Boolean type
8.4 Use logic and && to detect the existence and subsequent operation of an object or its properties. For example: Object && object.name && object.name = ' name ';
8.5 using logic or | | To provide default values for function parameters, also commonly used? The question mark condition operator provides a default value. For example: obj = obj | | {};
8.6 Use curly braces {} to define object literals, JSON data formats, and code blocks. For example: var obj = {};
8.7 Use the brackets [] to define the array literal, JSON data format, access to the array, and the access name is the attribute of the variable or special character. Example: Obj[this.index]
8.8 bitwise operations can be applied to a number of places: if you do not use intermediate variables to directly exchange two values, judge odd and even, MD5 encryption and so on.
Summary of JavaScript common operators and operators