JavaScript study notes four operators

One, the operator

Operators are also called operators. Through operations, one or more values ​​can be operated on and the result can be obtained.
For example, typeof is an operator that can get the type of a value. It returns the type of the value as a string

Two, arithmetic operators

  • When performing operations on a value other than Number, the value is first converted to a number type, true is 1, and false is 0.
  • For any number and NaN, the value obtained is NaN.
  • When any value is subtracted, multiplied, or divided , the value will be converted to the Number type first . The return value type after the operation is also the Number type. We can use this feature to perform type conversion, and the value is -0; *1; /1 The result obtained remains unchanged, and the value type is converted to the Number type. The principle is the same as the Number() function, and it is easier to use.
            var a="123";
			var b=a-0;//var b=a*1;var b=a/1;减法,乘法,除法操作
			console.log(b);//结果是123
			console.log(typeof b);//数据类型是number
  • When adding any value to a string , first convert the non-string type to a string type , and add any value type to an empty string "" to convert any value type to a string String type. This is an implicit type conversion, which is done automatically by the browser, and actually calls the String() function.
            var a=123;//number类型
			a=a+"";//和空串相加
			console.log(a);
			console.log(typeof a);//输出类型为String类型

(1) Add +:

  1. Number + number: Add two numbers and return the result
  2. String + String: Combine two strings into one string, and the return type is still the string type, which is called "Spelling String". Plus sign + can wrap
  3. Any value + string: Add any value to a string, first convert it to a string, and then perform a spelling operation with the string. The return value type is String.
            var a=123;
			var b="a="+a;
			console.log(b);//a=123
			console.log(typeof b);//类型是String类型
练习:运算从左到右
            var a=1+2+"3";
			console.log(a);//33
			var b="1"+2+3;
			console.log(b);//123

(2) Subtract -:

  1. Number-Number: Subtract two numbers and return the result.
  2. Number-string: Convert a string to a number, and then perform a number-number operation. If the string is not a number, the output result is NaN.
            var a=100;
			var b=a-"1";
			console.log(b);//结果为99

(3) Multiply *:

  1. Number * number: two values ​​are multiplied and the result is returned
  2. Digital string: Convert a string to a number, and then perform digital calculations.

(4) Divide /:

  1. Number * number: divide two values ​​and return the result
  2. Number/string: Convert a string to a number, and then perform a number/digital operation.

(4) Take the modulus and take the remainder%:

  1. Divide two numbers and take the remainder

Three, unary operator

Unary operator: only one operand is required

+Positive sign: The positive sign does not affect the
number-Negative sign: The negative sign can reverse the number

For non-Number type numbers, first convert them to Number type, and then perform operations.

            var a=true;
			var b=-a;//先将true转变为Number类型1
			console.log(b);//结果是-1

You can use "+ plus sign" for other data types to make them the Number type. The principle is the same as the Number() function

           var a="123";//字符串类型
			var b=+a;//用“+”
			console.log(b);//123
			console.log(typeof b);//number

Four, self-increasing and self-decreasing

(1) Self-increment ++: Through self-increment, the variable can be increased by 1 on the basis of itself

  1. After a variable is incremented, the value of the original variable will be incremented immediately by 1
  2. There are two types of self-increment: rear ++ (a++) and front ++ (++a)

Whether it is a++ or ++a, the value of the original variable will be automatically incremented immediately. The
difference is that the values of a++ and ++a are different. The value of
a++ is equal to the value of the original variable (the value before the increment). The value of
++a Equal to the new value of the original variable (value after self-increment)

var a=1;
console.log("a++="+ a++);//先赋值后+  结果是a++=1
console.log(a);//2
console.log("++a="+ ++a);//先+后赋值 结果是++a=3
console.log(a);//a=3
var a=10;
//10+12+12=34
console.log(a++ + ++a + a);

(2) Self-decrement -: Through self-decrement, the variable can be reduced by 1 on its own basis

  1. After decrementing a variable, the value of the original variable will be decremented immediately by 1
  2. There are two types of decrement: post-(a-) and pre-(--a)

Whether it is a– or –a, it will immediately decrement the value of the original variable by 1. The
difference is that the values ​​of a– and --a are different
. The value of a– is equal to the value of the original variable (the value before decrement)
. The value is equal to the new value of the original variable (the value after decrement)

Five, logical operators

JS provides us with three logical operators

(1)! Not: can perform a not operation on a value

  • The so-called non-operation is to reverse a Boolean value, true to false, false to true
  • If a value is reversed twice, the value remains unchanged.
  • If a non-boolean value is negated, it will be converted to a boolean value, and then reversed, so we can use this feature to convert another data type to a boolean value type, which can be an arbitrary data type Reverse twice to convert it to a boolean

(2)&&与:

  • You can AND the values ​​on both sides of the symbol and return the result
  • Operation rules: the result is true if both sides are true, and the result is false as long as one of the two sides is false.
  • The "and" in JS is a short-circuit AND, if the first value is false, you don't need to worry about it later, and the result is directly returned to false

(3)||or:

  • You can AND the values ​​on both sides of the symbol and return the result
  • Operation rules: If both sides are false, the result is false. As long as there is a true on both sides, the return result is true.
  • The "and" in JS is a short-circuit OR. If the first value is true, you don't need to worry about it later, and the result is directly returned to true.

(4) AND or OR operation of non-Boolean value

  • For non-Boolean AND or OR operations, first convert it to a Boolean value, then perform the operation and return to the original value.
  • AND operation: If both values ​​are true, the following value is returned
var a=1 && 2;//转换为布尔值true&&true
console.log(a);//两个值都为true,结果返回后面的2
  • AND operation: If one of the two values ​​is false, then the value of false is returned
var a=0 && 1;//false && true 返回0
var a=1 && 0;//true && false 返回0
var a=NaN && 0;//false &&false 返回NaN
console.log(a);
  • Or operation: if both values ​​are true, the first value is returned
  • Or operation: if one of the two values ​​is true, it returns the value of true
	var a=1|| 2;//true||true 返回1
	console.log(a);
	var a=0 || 1;//false || true 返回1
	console.log(a);
	var a=1 || 0;//true || false 返回1
	console.log(a);
	var a=NaN || 0;//false ||false 返回0
    console.log(a);
  • Or operation: string empty string is true, the rest are false
var a=""||"hello";//false || true
console.log(a);//结果返回hello

Six, assignment operator

  1. Equal sign =: Assign the value on the right side of the symbol to the left side of the symbol
  2. Add is equal to +=: a+=1 is equivalent to a=a+1.
  3. Subtraction is equal to -=: a-=1 is equivalent to a=a-1.
  4. Multiplication is equal to * =: a* =1 is equivalent to a=a*1.
  5. Divide equal to /=: a/=1 is equivalent to a=a/1.
  6. Modulus is equal to %=: a%=1 is equivalent to a=a%1.

Seven, relational operators

Provides a relational operator to compare the magnitude relationship between two values. If the relationship is established, it returns true, and if the relationship is not established, it returns false.

(1) Comparison of numerical situation

  1. Greater than sign >: Determine whether the value on the left side of the symbol is greater than the right side, if the relationship is established, return true, if the relationship is not established, return false
var a=5>10;
console.log(a);//false
  1. Greater than or equal sign >=: Determine whether the value on the left side of the symbol is greater than or equal to the right side, if the relationship is established, return true, if the relationship is not established, return false.
  2. Less than sign <: Determine whether the value on the left side of the symbol is less than the right side, if the relationship is established, return true, if the relationship is not established, return false.
  3. Less than or equal to <=: Determine whether the value on the left side of the symbol is less than or equal to that on the right side. If the relationship is established, return true, and if the relationship is not established, return false.

(2) Non-numerical situations

  1. When comparing non-numerical values, first convert the non-numerical value to a numerical value, and then perform the comparison.
var a=1>true; //true转换为1
console.log(a);//false

var a=1>null; //null转换为0
console.log(a);//true

var a=1>"你好"; //你好转换为NaN
console.log(a);//false,字符串转换为数字是NaN,无法比较,一律返回false
  1. If both sides of the symbol are strings, they will not be converted to numbers for comparison, but the Unicode codes in the strings will be compared separately .
var a="1" < "5";//true
var a="11" < "5";//true
console.log(a);
  1. The comparison character code is compared bit by bit. If the first bit is compared, there is no need to compare later. If the first bit is the same, then compare the next bit, which can be used to compare English. Comparing Chinese is meaningless.
var a="a"<"b";//a的字符编码小于b
console.log(a);
		
var a="abc"<"b";//abc只比较第一位a
console.log(a);
  1. If you compare two string-type numbers, you may get unexpected results.
    Note: When comparing two string-type numbers, you must perform the conversion "+"
var a="1232142525256"< +"5";
		console.log(a); //false

8. Unicode code table

Use escape characters to enter Unicode encoding in the string: \u four-digit encoding

console.log("\u0054");//结果是1

Use Unicode encoding in the web page: encoding; the encoding required here is decimal, and the number provided in the unicode encoding is hexadecimal. When used in the web page, you need to convert the hexadecimal to decimal.

Nine, equality operator

(1) Equality operator:

Compares whether two values ​​are equal, and returns true if they are equal, otherwise returns false. Use == to do equality operations

  1. When using == to compare two values, if the types of the values ​​are different, it will automatically perform type conversion, convert them to the same type, and then compare and judge.
    Not necessarily all converted to number type
console.log("1"==1);//字符串转换成1
console.log(null==0);//特殊情况,null未转换成number
  1. undefined is derived from null, so when these two values ​​are equal, it will return true
console.log(undefined==null);//true
  1. NaN is not equal to any value, including itself. You can use the isNaN() function to determine whether a value is NaN.
var a=123;
console.log(isNaN(a));//false

(2) Inequality operator:

Unequal is used to determine whether two values ​​are not equal. If they are not equal, return true, and if they are equal, return false.
use! = To do unequal operations.
Unequal will also perform automatic type conversion on variables, and it will also return false if they are equal after conversion.

(3) Congruent ===

Used to determine whether two values ​​are congruent, similar to equality, except that it will not do automatic type conversion.
If the two values ​​are of different types, return false directly

(4) Not waiting! ==

It is used to judge whether two values ​​are not equal, which is similar to inequality, except that it does not perform automatic type conversion.
If the two values ​​are of different types, return true directly

Ten, conditional operator

Conditional operator is also called ternary operator

(1) Grammar: Conditional expression? Statement 1: Statement 2

(2) Process of execution:

When the conditional operator is executed, the conditional expression is first evaluated.
If the value is true, statement 1 is executed and the execution result is returned;
if the value is false, statement 2 is executed and the execution result is returned.

var a=10,b=20;c=50;
		//获取a和b中的最大值
		var max=a>b?a:b;
		//获取max和c中的最大值
		max=max>c?max:c;
		console.log("max="+max);
		
//这种写法不推荐,不方便阅读
var max=a>b?(a>c?a:c):(b>c?b:c);
		console.log("max="+max);

If the result of the conditional expression is a non-Boolean value, it will be converted to a Boolean value and then judged.

Eleven, the precedence of operators

(1) Comma, operator: you can split multiple statements, and generally can be used when declaring multiple variables.
var a,b,c;
(2) Operator priority: as in mathematics, first multiply and divide and then add and subtract.
In JS, there are changes in symbolic priority. In the table, the higher the priority, the first calculation, and the same priority is calculated from left to right. If the priority is not clear, you can use () to change the priority.

Guess you like

Origin blog.csdn.net/weixin_45636381/article/details/112568085