JS expressions and operators in "JS learning notes"

Posted by yellowzm on Wed, 05 Jan 2022 11:47:05 +0100

1, What are expressions and operators

Expression: an expression consisting of operands and operators.

Classification of expressions: arithmetic, relation, logic, assignment and synthesis.

2, Arithmetic expression

2.1 arithmetic operators

significanceoperator
plus+
reduce-
ride*
except/
Surplus%

It is recommended to use () to enforce priority!

  • +It has two functions of "addition" and "hyphen". If the operands on both sides of + are numbers, it is "addition", otherwise it is "hyphen"

2.2 implicit type conversion

If an operand participating in a mathematical operation is not of numeric type, JS will automatically convert the operand to numeric type.

3 * '4';		// 12
true + true;	// 2
false + 2;		// 2
3 * '2 day'; 	   // NaN
3 + '2 day'; 	   // '32 days'
3 + null;		// 0
3 * '';			// 0
3 * ' ';		// 0
3 + '';			// '3'
3 + ' ';		// '3 '

The essence of implicit conversion is to automatically call the Number() function inside JS

2.3 relevant IEEE754

In JS, the mathematical operation of some decimals is not very accurate.

0.1 + 0.2;	// 0.30000000000000004

JS uses IEEE754 binary floating point arithmetic standard, which will cause "loss of precision" in some individual decimal operations.

Note: not all decimal operations will have accuracy problems, only a few.

Almost all high-level languages use the IEEE754 binary floating-point arithmetic standard.

IEEE754 binary floating-point arithmetic standard is the underlying compilation standard of the computer. You can understand it!

[solution]

When performing a decimal operation, the toFixed() method of a number is called to retain the specified number of decimal places.

toFixed() accepts a parameter that preserves the number of decimal places in parentheses.

(0.1 + 0.2).toFixed(2);				// '0.30'
Number((0.1 + 0.2).toFixed(2));		// 0.3

toFixed() follows the principle of "rounding".

(0.9945).toFixed(3);	// "0.995"

2.4 power sum open radical sign

JS does not provide operators for power operation and open radical. You need to use the relevant methods of the Math object for calculation.

Math.pow(a, b): find the B power of A.

Math.sqrt(a): find the square root of A.

Math.pow(2, 3);		// 8
Math.pow(3, 2);		// 9
Math.sqrt(81);		// 9
Math.sqrt(-81);		// NaN

2.5 rounding up and rounding down

Math.ceil(): round up.

Math.floor(): rounded down.

Note: the standard of up and down is: the positive direction of X axis is up!

Negative -- 0 -- > positive

Math.ceil(2.4);			// 3
Math.floor(2.4);		// 2

Math.ceil(-2.4);		// -2
Math.floor(-2.4);		// -3

Math.ceil(2);			// 2
Math.floor(2);			// 2		

3, Relational expression

3.1 relational operators

significanceoperator
greater than>
less than<
Greater than or equal to>=
Less than or equal to<=
be equal to==
Not equal to!=
All equal===
Not all equal to!==

3.2 equality and congruence

The two equal sign = = operators do not compare the types of values. They will compare whether the values are equal after implicit conversion.

The three equal sign = = = operators compare not only whether the values are the same, but also whether the types are the same.

5 == '5';		// true
5 === '5';		// false
1 == true;			// true
1 === true;			// false

0 == false;			// true
0 === false; 		// false

0 == undefined;		// false
0 === undefined;	// false

undefined == null;	// true
undefined === null;	// false

null and undefined are compared with = = involving implicit cast, which is specified in the ES5 specification.

===The comparison is false because null is different from undefined.

3.3 NaN is not self-contained

As a special numeric value, NaN also has special results when compared with = =.

NaN == NaN;			// false
NaN	=== NaN;		// false

[how to judge that a variable value is NaN]

The isNaN() function can be used to determine whether the variable value is NaN.

isNaN(NaN);		// true
isNaN(5);		// false

However, isNaN() is not easy to use. Its mechanism is that as long as the execution result of the variable passed into Number() is NaN, the isNaN() function will get true.

isNaN(undefined);	// true
isNaN('3 day');	   // true
isNaN(null);		// false

3.4 there is no continuous ratio in JS

For example, the wording of 3 < = a < = 15 is wrong. It should be: a > = 3 & & A < = 15.

4, Logical expression

4.1 logical operators

significanceoperator
wrong!
And&&
or||

4.2 non operation

! Means "not", which can also be called "inverse operation".

! Is a unary operator that requires only one operand.

The result of the inverse operation must be a Boolean value.

!true;			// false
!false;			// true
!0;				// true
!undefined;		// true
!'';			// true
!' ';			// false
!null;			// true
!'imooc';		// false

!! A Boolean attribute commonly used to determine a value.

!!true;		// true
!!0;		// false
!!'';		// false
!!' ';		// true
!!'imooc';	// true

4.3 and operation

&&Is the binocular operator.

Core: all truth is true, and false is false.

4.4 or operation

||Is the binocular operator.

Core: all false is false and true is true.

4.5 short circuit operation

&&And |, both belong to the "short circuit operator".

(1) & & short circuit operation

Since the core of && operation is: "all truth is true, and false is false", so:

  • If a is true in a & & b, the value of the expression is determined by b
  • If a in a & & B is false, the value of the expression is determined by A
3 && 6;				// 6
undefined && 15;	// undefined
15 && undefined;	// undefined
null && 2;			// null
'' && 16;			// ''
NaN && undefined;	// NaN

(2) Short circuit operation

Since the core of 𞓜 operation is: "all false is false and true is true", so:

  • If a in a | B is true, the value of the expression is determined by A
  • If a in a | b is false, the value of the expression is determined by b
3 || 6;				// 3
0 || 6;				// 6
null || undefined;	// undefined
'a' || 'b';			// 'a'
NaN || null;		// null

4.6 priority of logical operation

Priority:! > & & >||

!true || true;		// true
3 && 4 || 5 && 6ï¼›  // 4

It is recommended to use () to specify priority.

5, Assignment expression

5.1 assignment operator

significanceoperator
assignment=
Quick assignment+=,-=,*=,/=,%=
Self increasing operation++
Self subtraction operation--

5.2 value generated by assignment operation

The assignment operation also produces a value, and the value after the equal sign will be used as the "value of the assignment operation".

var a;
console.log(a = 4);		// 4

This means that assignment operators can be used continuously.

var a, b, c;
a = b = c = 15;
console.log(a);		// 15
console.log(b);		// 15
console.log(c); 	// 15

Continuous assignment is not recommended in actual development!

5.3 quick assignment

The quick assignment operator indicates further operation based on the "original value".

5.4 self increasing and self decreasing operation

A + +: use first and then add++ a: Add it first and then use it.

A --: use first and then reduce-- a: First reduce and then use.

6, Operation sequence of synthesis operation

Non operation > mathematical operation > relational operation > logical operation

It is recommended to use () to specify priorities.

Topics: Javascript Front-end