One article to get all kinds of basic operators in js (arithmetic operators, relational operators, logical operators)

Table of Contents
1. Arithmetic Operators
2. Relational Operators
3. Logical Operators
4. Priority of Operators

One, arithmetic operators

  1. The common arithmetic operator
    +: is equivalent to the addition operation in mathematics; 222+333=555
    -: equivalent to the subtraction operation in mathematics; 666-333=333
    : equivalent to the multiplication operation in mathematics; 333 2=666
    /: It is equivalent to the division operation in mathematics; 666/333=2
    %: it is equivalent to the remainder operation in mathematics; 9%4=1
  2. When performing arithmetic operations on data of non-number type (string/boolean/undefined/null), it must be implicitly converted (through the Number() function) into a number before calculation
			console.log('5' - 3);//5-3=2
			console.log('5' * '3');//5*3=15
			console.log('30' / '5'); // 30/6=6
			console.log(5 - true); //5-1=4
			console.log(5 + false ); //5+0=5
			console.log(false - true); //0-1=-1			
			console.log(null + false ); //0+0=0
			console.log(null - 2); //0-2=-2
  1. When NaN (including the data converted to NaN by Number()) is calculated with other data, the result is always equal to NaN (except for the result of addition to a string).
			console.log('哈哈' - 5); //NaN Number('哈哈') = NaN
			console.log('5a' - 5); //NaN Number('5a') = NaN
			console.log(5 - {
    
    }); //  NaN Number({}) = NaN
			console.log(undefined %'2');//NaN Number(undefined) = NaN
			console.log('vbb3.6d' * 2); //NaN Number('vbb3.6d') = NaN
			console.log(NaN+2);//NaN 
  1. Special string addition- any data and string addition operation is in the process of spelling
		console.log('我是字符串呀'+10)//'我是字符串呀10'
		console.log('5'+true +6;//'5true6'
		console.log('5'-true +'6';// '5'-true = 4 ,4 和'6'拼串,'46'
		console.log(undefined+'2';//'undefined2'
		console.log(true+(null*2+'5')+undefined);// null*2 = 0, 'true05undefined'

Two, relational operators

  1. Common relational operators
    >, <, >=, <=, == (equal to),! = (Not equal to), == = (all equal to), !== (incomplete, etc.); the result of the relational operation is Boolean value

  2. Relational operations follow the rules
    a. Numbers are compared normally;
    b. Non-digital types are converted into numbers first and then compared
    . c. When comparing strings, the comparison is performed according to the number of digits.

   		console.log('5' > 3);//true  5>3
   		console.log('5' != 3);//true 5!=3
   		console.log(3 != 3);//false 
   		console.log('3' != 3);//false
   		console.log('3' !== 3);//true
   		console.log(false > 0);//false 0>0
   		console.log(true > -1);//true0>-1
   		console.log(''>-1);//true 0>-1
   		console.log('5' > '4');//true
   		console.log('5a' > 3);//true
   		console.log(' ' > '');//true
   		console.log(' ' > 0);//false 0>0
   		console.log('5a' < '5b');//true
   		console.log('9a' < '3z');//false

d. By default, NaN is not equal to any value, including itself.
e. By default, null and undefined are equal to each other, and also equal to itself; except that they are not equal to any other value.

		console.log(undefined > -1);//false
		console.log(null > -1);//false
		console.log(null == null);//false
		console.log(null == 0);//false
		console.log(null == false);//false
		console.log(null == '');//false
		console.log(null == undefined);//true
		console.log(undefined == undefined);//true
		console.log(undefined == 0);//false
		console.log(undefined == false);//false
		console.log(undefined == '');//false
		console.log(NaN == NaN);//false

f.==It only requires that the values ​​of the two data are equal.
g.===Full equality requires that the size and data type of the two data values ​​are equal.

		console.log('5' == 5);//true
		console.log(5 == 5);//true
		console.log('5' === 5);//false 数据类型不同
		console.log('5' === '5');//true

Three, logical operators (short-circuit expression)

1. Summary of the usage of logical operators
&& (and): If there is false, then it is true.
|| (or): If there is truth, it is true, and all false is false
! (Non): Invert operation
2. Practice

		console.log(5 > 3 && 5 > 7);//false 有假即假
		console.log(5 > 3 && 5 < 7);//true 都真才真
		console.log(5 > 3 && 5 < 7 && 3 != 3);//false
		console.log(5 > 3 || 5 > 7);//有真即真
		console.log(5 < 3 || 5 < 7);//false
		console.log(5 > 3 && 5 > 1);//true
		console.log(true && true && true);//true
		console.log(true && true);//true
		console.log(false && false && true);//false
		console.log(5 && 3);//3第一个值为真,返回第二个值
		console.log(5 && 0);//0
		console.log(0 && 5);//0 有假即假
		console.log('haha' && -5 && '');// 前两个都是true,返回最后一个,空字符串,看不见
		console.log('(*^__^*) 嘻嘻……' && ' ' && '小左同学');//'小左同学'
		console.log('5aaa' > 6 && !'yellowgreen' && 0 - 5 * (3 + 1));//false 第一个值为假,有假即假
		console.log(6 && !'yellowgreen' && 0 - 5 * (3 + 1));//false 第二个值为false,有假即假
		console.log(6 && 'yellowgreen');//yellowgreen
		console.log(!3);//false
		console.log(6 && 'yellowgreen' && 0 - 5 * (3 + 1));//-20 算数运算的优先级高于逻辑运算,先算出结果-20,前两个值为真,返回第三个值-20			  	
		console.log(!(5 < 3) && !5 < 3 && !undefined && null+'5');// null5 前3个都为true,返回拼串
		console.log(false || true || true);//true 有真即真
		console.log(5 || 3);//5有真即真
		console.log(!(5 < 3) || !5 < 3 || !undefined || null+'5');//true第一个值为真,有真即真不管后边
		console.log(5 < 3 || !5 < 3 && !undefined || null+'5');//先算&&,结果为true,有真即真
		console.log(5 && 3 || '哈哈' && 8);//3
		console.log(5 && (3 || '哈哈') && 8);//8
		console.log(0 || 5);//5

Fourth, the precedence of operators

  1. Unary operator (++-!)> Binary operator> Ternary operator
  2. The priority of binary operators: arithmetic operators (/ *) (+ -)> relational operators (> <>= <=)> logical operators (&& ||)

If there is an error, please feel free to correct it; if you have any questions, please leave a message for discussion.

Guess you like

Origin blog.csdn.net/xiaozuo144/article/details/109691838