Arithmetic operators, logic operators, relational operators
Arithmetic operators:
+ Addition - Subtraction * Multiplication / Division% Remainder (modulo)
var a = 1, b = 2;
a + b = 3
a - b = -1
a * b = 2
a / b = 0.5
a % b = 1
Although very similar to the math, but there are some different
var a = "1", b = "2";
a * b = 2
a / b = 0.5
a - b = -1
a + b = 12 // 只有加号不一样,加号有连接的意思;
+ Sign, has two meanings, the arithmetic addition between the first layer; a second layer connection string; implementation process, both sides of the first detection data type plus, if the character was found, then the character is connected to commencement
data type conversion: cast (parseFloat, number (), parseInt etc.), implicit conversion
Assignment operator:
= += -+ *= /+ %=
Operators | example | Equivalent to | Operation result |
---|---|---|---|
= | y = 6 | y = 6 | |
+= | and + = 1 | y = y+1 | y = 7 |
-= | and - = 1 | y = y-1 | y = 5 |
*= | and * = 2 | y = y*2 | y = 12 |
/= | and / = 2 | y = y/2 | y = 3 |
%= | and% = 4 | y = y%4 | y = 2 |
Relational operators:
< <= > >= == != ===
Operators | Explanation | example | Operation result |
---|---|---|---|
== | equal | 2 == 3 | false |
=== | Identically equal to (value and type have to do comparison) | 2 === 2 2 === “2” | true false |
!= | Is not equal, can also be written <> | 2 != 3 | true |
> | more than the | 2 > 3 | false |
< | Less than | 2 < 3 | true |
>= | greater or equal to | 2 >= 3 | false |
<= | Less than or equal | 2 <= 3 | true |
Conditions set up to output true, not true to false output
Logical Operators
Operators | Explanation | example | Operation result |
---|---|---|---|
&& | Logic (and) | x = 2;y = 6;x>5 && y>5 | FALSE |
|| | Logical OR (or) | x = 2;y = 6;x>5 || y>5 | TRUE |
! | Non-logic, fetch logic opposite | x = 2;y = 6;!(x > y) | TRUE |
String concatenation operator "+"
Example 1: using the logical operators && output x = 4; y = 8; x> 5 && y> 5
var x=4,y=8;
console.log(x>5&&y>5); // false
Example 2: Operator ||
console.log(x>5 || y>5); // true
Example 3: using the logical operators Output a = 2; b = 6; (a> b)!!
// 取与结果相反的一面
var a=2,b=6;
console.log(!(a>b));
Additional supplements
Increment (+) / decrement (-) operator
a ++ indicates an increase in a 1 on the basis of the original
Equivalent to: a = a + 1;
a--表示在a原有的基础上减小1
Equivalent to: a = a-1;
Example 1: a = 1; a ++ output value
var a =1;
// 先输出,后运算
// console.log(a++); //1
a++;
console.log(a); //2
var a =1;
// 先赋值,再运算
var b = a++;
console.log(b); // 1
Example 2: b = 2; b- value output
var c=2;
c--;
console.log(c);
Thoughts: ++ a and -a it?
If the front is the first operation after operation
Example 3: a = 1; ++ a value of output
var a=1;
console.log(++a);
Example 4: a = 2; the value output -a
console.log(--a);
Foot care
Since the increase before the increment are essentially different, they are all the same for their own plus points 1,
difference is that
the former is to increase self plus one, and then use the value of the operand
after the increment is to use the value of the operand , plus 1