[Note: JS basis operators]

1, arithmetic operators

Description about the arithmetic operators:

1). In addition to addition, when the value of the non-performing operations of type Number, Number will then be converted to do arithmetic.
2) do while adding, if the two strings are added, it will do to fight string operation, the two characters are connected as a string.
3) Any string values and do the addition, will be converted to a string, then the string fight.
4) Any value calculation had to do and NaN NaN.

  1. "+": Adding two values ​​and returns the result
result = 456 + 789;
result = "你好" + "大帅哥";
result = 123 + "1";
result = true + "hello";
result = 123 + ""; //空字符串转换为Number类型之后为0
  1. "-": subtraction of two values ​​and returns the result
result = 100 - 5;
result = 100 - true;  //相当于100-1
  1. "*": Multiplication of the two values ​​and returns the result
result = 2 * 2;
result = 2 * "8";
result = 2 * undefined; //undefined转换为Number类型之后为NaN
result = 2 * null; //null转换为Number类型之后为0
  1. "/": Division of the two values ​​and returns the result
result = 4 / 2;
result = 3 / 2;
  1. "%": Two values ​​and returns the result modulo operation
result = 4 % 2;  //输出结果为0
result = 3 % 2;  //输出结果为1
  1. "++": increment can make on the basis of the original value of the variable is incremented by one

Increment can be used before and after ++ (++ a) ++ (a ++)
either (++ a) or (a ++) would immediately increase from the original variable 1

The difference is (++ a) and value (a ++) is different:
(++ a) the value is the new value of the variable (from value increase), equivalent has been performed twice (a ++)

var a = 2;
result = ++a;  //输出结果为3

A ++ original value is the value of the variable (the value before the increment)

var a = 2;
result = a++;  //输出结果为2
result = a++;  //第二次执行输出结果才会为3
  1. "-": Save from the original value can be made variable on the basis of the decremented 1

Decrement may be used prior to - (- a) or after - (a-)
either (-a) or (A-) will immediately cause the original variable is decremented 1

The difference is "--a" value and "a--" are different:
-a value is the new value of the variable (the value after decrement), equivalent has been performed twice (A-)

var a = 2;
result = --a;  //输出结果为1

A- original value is the value of the variable (value before decrementing)

var a = 2;
result = a--;  //输出结果为2
result = a--;  //输出结果为1

2, a unary operator

Unary operators, in the mathematical equivalent of a value into a positive or negative, only one operand

  1. Positive sign: a positive sign will not have any impact on the numbers.
  2. Negative: negative sign can be inverted digital negative sign.
For the type of value of the non Number

1). It will be converted to Number, then in operation.
2) may be used for a different data type + to convert it as a number.

3, logical operators

! Non- : a value may be used for non-operation, a NAND operation is called a Boolean value to negate operation value,

The categories : true becomes false, false becomes true

Description of non-operation

1). If a value is inverted twice, it does not change.
2). If the elements of the non-Boolean value, then converts it to a Boolean value, and then negated.
3). So we can use this feature to a different data type to a Boolean value.
4) may be taken as an arbitrary two inverse data type, to be converted to a Boolean value.

!true; //输出false
!false; //输出ture

And && : && can symbol values on both sides of the operation and return the result

About the operation instructions

1). As long as there are two values returns a false value is false.
2) Only when both values are true, it will return true.
3). JS in the "with" and belonging to a short circuit, if the first value is false, will not see the second value.
4) If the first false, the direct return to the first value.

//如果两个值都是true则返回true
var result = true && true;
//只要有一个false,就返回false,所以下面三个的输出都是false
result = true && false;
result = false && true;
result = false && false;
true && alert("弹出框···");//第一个值为true时,才会检查第二个值,所以会输出后面的弹出框
false && alert("弹出框···");//第一个值为false时,不会检查第二个值,不会输出后面的弹出框

Or || : || can be symbol values or both sides of the operation and returns the result

Or instructions on operation

1) The two values as long as a true, returns true.
2) If both values are false, it returns false.
3). JS in "or" belongs to, or a short circuit.
4) If the first value is true, the second value is not checked.
5) If the first value is false, then the second value is returned.

//两个都是false,则返回false
result = false || false;
//只有有一个true,就返回true,所以下面三个的输出都是true
result = true || false;
result = false || true ;
result = true || true ;
false || alert("弹出框···");//第一个值为false时,则会检查第二个值,所以会有弹出框
true || alert("弹出框···");//第一个值为true,则不再检查第二个值,所以不会有弹出框
&&, || not a Boolean value note

When the non-boolean OR operation for, will first convert it to a Boolean value, then operation returns the original value and

4, the assignment operator

  1. "=": The right side of the symbol values ​​may be assigned to the left of the Symbol
  2. "+ =": A + = 5 is equivalent to a = a + 5
  3. "- =": a - = 5 is equivalent to a = a - 5
  4. " =":. 5 is equivalent to A = A = A . 5
  5. "/ =": A / = 5 is equivalent to a = a / 5
  6. "% =": A% = 5 is equivalent to a = a% 5

5, relational operators

Can compare the relationship between two values ​​by the relational operator, it will return if the relation holds true, if the relationship is not established it returns false

  1. ">": No value is greater than judgment value is greater than the symbol to the left of the right.
  2. "> =": Determining whether a value greater than or equal sign left of greater than or equal to the right.
  3. "<": Less than the number of the left symbol determination value is less than the right value.
  4. "<=": Less than or equal to the left symbol determination value is less than or equal to the value of the right side.
var result = 5 > 10;//false
result = 5 > 4; //true
result = 5 > 5; //false
result = 5 >= 5; //true
result = 5 >= 4; //true
result = 5 < 4; //false
result = 4 <= Na; //true
Non-value

1). When compared for non-numeric, then converts it to a digital comparison.
2). If both values are strings of symbols, it will not be converted to a digital comparator, the comparator will encode each Unicode characters in the string.
3). Equality operator to compare the two values are equal, if equal returns true, otherwise returns false.
4) Any NaN values and make any comparisons are false

console.log("1" < "5"); //true
console.log("11" < "5"); //true
//比较两个字符串时,比较的是字符串的字符编码
console.log("a" < "b");//true
//比较字符编码时是一位一位进行比较
//如果两位一样,则比较下一位,所以借用它来对英文进行排序
console.log("abc" < "bcd");//true
//比较中文时没有意义
console.log("戒" > "我"); //true
//如果比较的两个字符串型的数字,可能会得到不可预期的结果
//注意:在比较两个字符串型的数字时,一定一定一定要转型
console.log("11123123123123123123" < +"5"); //true
  1. "==": equal to the operator to do
    when using == to compare two values, if the values of different types, the type is automatically converted, convert it to the same type and in the comparison.
console.log(a == 4); //false
console.log("1" == 1); //true
console.log(true == "1"); //true
console.log(null == 0); //false
/*
 * undefined 衍生自 null
 * 	所以这两个值做相等判断时,会返回true
 */
console.log(undefined == null);
/*
 * NaN不和任何值相等,包括他本身
 * 可以通过isNaN()函数来判断一个值是否是NaN
 * 如果该值是NaN则返回true,否则返回false
 */
console.log(NaN == NaN); //false
  1. "! =": Is not equal to the value used to determine whether the two are not equal, returns true if not equal, otherwise false.
    Use! = Do not equal operator will not equal the variable automatic type conversions, it also equal returns false if the conversion.
console.log(10 != 5); //true
console.log(10 != 10); //false
console.log("abcd" != "abcd"); //"abcd"转换为Number类型时为NaN,所以会输出false
console.log("1" != 1);//false
  1. "===": congruent congruent used to determine whether the two values, which are equal and similar, except that it does not do automatic type conversions, if the two values ​​of different type, directly returns false.
console.log(123 === 123);//false
console.log("123" === 123);//false
console.log(null === undefined);//false
  1. "==!": Insufficiency used to determine whether two values ​​are not congruent, and unequal similar, except that it does not do automatic type conversion, if the value of two different types, direct returns true.
console.log("123" !== 123);//true
console.log(null !== undefined);//true

6, the ternary operator

? ** Syntax: ** 1 conditional expressions statement: Statement 2;

Execution of the process:

1) Condition operator when executed, first conditional expressions are evaluated:
2) If the value is true, execute the statement 1, and returns an execution result;.
3) If the value is false, executing the statement. 2, and returns the execution result;
4) If the condition expression evaluates to a non-Boolean value, which will then be converted to Boolean operations.

var a = 300;
var b = 143;
a > b ? alert("a大"):alert("b大");
//因为字符串转Boolean类型会为true,所以
"hello"?alert("语句1"):alert("语句2");
Published 20 original articles · won praise 11 · views 1751

Guess you like

Origin blog.csdn.net/qq_16221009/article/details/102868438