js Grammar Basics (4)

4. Operators

4.1. What is the operator?

And other symbols to represent - operator is used to represent specific rule calculation symbols such as mathematical calculations of arithmetic operation is a specific rule, we are using "* / +"

4.2. Classification operator

4.2.1 arithmetic operators

Arithmetic operators are: + - * /% + -, which is mainly used as digital symbols other aspects of operation. Wherein a modulo operation difficulties belonging%

console.log(97%10);//输出7
console.log(100%10);//输出0
console.log(-97%10); //输出-7
console.log(97%-10); //输出7

Summary:% indicates finding the remainder of dividing two numbers, symbols and consistent dividend

Increment decrement operator:

var a=1;
var b=a;
a=b++;  //a=1 b=2 这一步的时候先把b赋值给a 所以a=1,b自己加1 所以b=2
b=a++;  //b=1 a=2 //这一步的时候先把a=1赋值给b 覆盖来上一步b=2,所以b又等于1,然后a自己加1 a=2
a=++b;  //a=2 b=2 //这一步先让b自加1等于2 然后赋值给a
console.log(a,b); //输出结果 2 2

Summary: 1, ++ means add their own 1, - represents subtraction 1,2 own, on the front: the first self-operation to the arithmetic operator and other .3, on the back: the first operators and other operations, then their operation

4.2.2. Comparison Operators

Comparison operator is the size comparison between the two data are equal, the finally obtained a Boolean value. Comparison operators include: == =>> = <<= === (congruent) == (not identical to)!!

var a=5;
var b=6;
console.log(a>b);//false
console.log(a<b);//ture

Summary: comparison operators, note that the difference between == and ===, the two sides of the equal sign just compare values ​​for equality, not only to compare both sides of the equal sign three values ​​are equal, but also on both sides type is the same, only the type and value are the same and we have to return true

var a=5;
var b = "5";
console.log(a==b); //true
console.log(a===b);//false

4.2.3. Ternary operator

Also called ternary operator conditional operator or ternary operator, it is used to make the determination condition, a syntax structure: Condition? Expression 1: Expression 2

4>5?console.log(4):console.log(5);//5
5>4?console.log(4):console.log(5);//4

Summary: ternary operator made? : Composition, if the previous question mark is true, is executed after the question mark, the code number before the colon, the colon is question mark in front of the code, if false, the implementation

4.2.4. Logical Operators

Logical operator is mainly used to determine one or more conditions are satisfied at the same time or one of them. Including: & (and) || (or) (non)!

  console.log(5>3 && 5<3); // true && false ;最终结果false
  console.log(5>3 && 4>3); //true && true ;最终结果 true
  console.log(5<3 && 5>4);  //false && true ;最终结果false
  console.log(5<3 && 4<3); //false && false ;最终结果false

  console.log(5>3 || 5<3); // true && false ;最终结果true
  console.log(5>3 || 4>3); //true && true ;最终结果 true
  console.log(5<3 || 5>4);  //false && true ;最终结果true
  console.log(5<3 || 4<3); //false && false ;最终结果false
   console.log(!5>3 ); //!ture; 最终结果false

Summary: 1, && represents, and is meant, that both sides of the operator must be satisfied before the condition is true, as long as there is a false value, the overall result is false; 2, or represents || meaning, that both sides as long as one to meet the conditions, then the entire result is true if both sides are false when it is false; 3,! Represents the inverse of the original true negated false, false is the inverse of the original true; 4, simple notation: false false &&, || there really is true; 5, logical operator of a short circuit occurs, when the previous code is false && when && behind the code is not executed, when the previous || true when the code behind the code does not execute ||

Screw classroom video lessons Address: http://edu.nodeing.com

Guess you like

Origin www.cnblogs.com/dadifeihong/p/12027540.html