1. Operators
Operator (operator), also called operator, for implementing the function assignment symbols, performs arithmetic and comparing the like.
JavaScript operators are commonly used:
l arithmetic operators
l increment and decrement operators
l Comparison operators
l logical operators
l assignment operator
2. arithmetic operators
2.1 Arithmetic Operators Overview
Concepts: arithmetic symbols used for performing two arithmetic variables or values.
Operators | description | Examples |
+ | plus | 10+20=30 |
- | Less | 10-20=-10 |
* | Multiply | 10*20=200 |
/ | except | 10/20=0.5 |
% | Take the remainder (modulo) | Returns the division remainder = 1 2 9% |
2.2 floating point precision issues
Most precision floating point value is 17 decimal places, but during which arithmetic accuracy is far less than an integer.
var result = 0.1 + 0.2; // result is not 0.3, but: 0.30000000000000004
the console.log (0.07 * 100); // result is not 7, but: 7.000000000000001
So: do not directly determine whether two floating-point numbers are equal!
2.3 expressions and return values
Expression: The combination of the resulting arrangement is of significant numbers, operators, and other variables to be able to obtain the value of
simple understanding: a digital expression, operators, variables, and other components
The final expression will have a result, return it to us, we become the return value
3. The increment and decrement operators
3.1 Overview of increment and decrement operators
If necessary to repeatedly add or subtract a variable number, you can increment (+) and decrement (-) operator to complete.
In JavaScript, increment (++) and decrement (-) may be placed in front of variables can be placed after the variable. When placed in front of a variable, we may call the pre-increment (decrement) operator, when placed after the variable, we may call post-increment (decrement) operator.
Note: The increment and decrement operators and variables must be used in conjunction.
3.2 increment operator
3.2.1 Pre-increment operator
++ num front increments, 1 is from Canada, similar to the num = num + 1, but ++ num much easier to write.
Use formulas: the first from Canada, the return value
var num = 10;
alert (n ++ I + 10); // 21
3.2.2 Post-increment operator
After num ++ incrementation is self plus one, like num = num + 1, but num ++ much easier to write.
Use formulas: first return to the original value, after the self-imposed
var num = 10;
alert (10 + num ++); // 20
3.2.3 pre-increment and post-increment Summary
pre-increment and post-increment operator can simplify the preparation of the code, so that the value of the variable + 1 than before writing easier
when used alone, the same operation results
when other code associated with the execution result will be different
Rear: first original value calculation, since the addition (after ancestors hexyl)
l pre: first incremented and the operation (the first descendants have)
l development time is often used post increment / decrement, and an exclusive line of code, for example: num ++ ; or num--;
4. Comparison Operators Overview
4.1 Comparison Operators Overview
Concept: comparison operators (relational operator) are compared two data used for the operator, the comparison operation, returns a Boolean value (true / false) as a result of the comparison operation.
Operator name | Explanation | Case | result |
< | Less than sign | 1<2 | true |
> | Greater than sign | ,>2 | false |
>= | Greater than or equal sign (greater than or equal to) | 2>=2 | true |
<= | Less number (less than or equal) | 3<=2 | false |
== | Sentenced equal sign (will transition) | 37==37 | true |
!= | Sign of inequality | 37!=37 | false |
=== !== | It requires congruent values and data types are consistent | 37==='37' | false |
4.2 Summary =
symbol | effect | usage |
= | Assignment | The right to the left |
== | Judge | Determining whether the same value on both sides (note that at this time there is implicit conversion) |
=== | congruent | Both sides of the determination value and the data type is the same as |
console.log(18 == '18');
console.log(18 === '18');
The logical operators
5.1 Logical Operators Overview
Concept: logical operators are used to perform Boolean operations on values, the return value is a Boolean value. Analyzing later developed for a plurality of conditions is often
Logical Operators | Explanation | Case |
&& | "Logical and" short "and" and | true && false |
|| | "OR", referred to as "or" or | true || false |
! | "Logical NOT", referred to as "non" not | !true |
5.2 Logical AND &&
Both sides are true return only true, false otherwise
5.3 logical or ||
Both sides are false only returns false, otherwise, are true
5.4 Logical NOT!
Logic NOT (!) Operator also called negated, contrary to take a Boolean value, such as the opposite true value is false
var = tomorrow! true;
console.log (always); // false
5.5 short-circuit operation (logic interrupts)
Short circuit operation principle: When there is more than one expression (value), the value of the expression on the left when the results can be determined, no longer continue operation value of the expression on the right;
5.5.1 logic.
syntax: expression 1 && expression 2
If the first expression evaluates to true, the expression returns 2
If the first expression evaluates to false, the expression returns 1
console.log( 123 && 456 ); // 456
console.log( 0 && 456 ); // 0
console.log( 123 && 456&& 789 ); // 789
5.5.2. Logical or
syntax: expression expression 1 || 2
If the first expression is true, the expression returns 1
If the first expression evaluates to false, the expression returns 2
console.log( 123 || 456 ); // 123 console.log( 0 || 456 ); // 456 console.log( 123 || 456 || 789 ); // 123
var num = 0;
console.log(123 || num++);
console.log(num);
6. assignment operator
Concept: the data for the operator assigned to a variable.
Assignment Operators | Explanation | Case |
= | Direct assignment | var userName = 'I value'; |
+=、-= | Addition, subtraction and then assign a number | var age = 10; age + = 5; // 15 |
*=、/=、%= | Multiplication, division, modulo assignment after | var age = 2; age * = 5; // 10 |
Age = 10 Var;
Age Tasu = 5; // equivalent Yu = Age Tasu Age 5;
Age - = 5; // equivalent Yu
age = age - 5; age * = 10; // equivalent Yu age = age * 10;
7. Operator Precedence
priority | Operators | order |
1 | Parentheses | () |
2 | Unary operators | ++ -- ! |
3 | Arithmetic operators | After the first * /% + - |
4 | Relational Operators | > >= < <= |
5 | Equality operators | == != === !== |
6 | Logical Operators | After the first && || |
7 | Assignment Operators | = |
8 | Comma operator | , |
unary operators inside the non-priority logical high
logic high priority than logical or