This is the 8th day of my participation in the August Text Challenge.More challenges in August

Arithmetic operator

+ - * / % ()

Abnormal case 1: Operations involving special value literals

  • Operations involving nans: All results are nans

  • Infinity operations, depending on the situation, such as:

5/Infinity=0
5%Infinity=5
Infinity%5=NaN
Infinity+5=Infinity
Infinity / Infinity = NaN
Infinity - Infinity = NAN
Infinity % Infinity = NAN
Copy the code

Special value operations are not used in the workplace and have no practical application, but be aware of them in case you come across them in an interview

Abnormal case 2: Other types of data are involved in mathematical operations.

  • + operations involving strings (including characters on only one side of the sign) : the + becomes a hyphen to concatenate the whole string.

Such as:

        var a = 3 - "36" % 5 + "2" - 2
        console.log(a)
        var b = "36" % 5
        console.log(b)
        var c = 3 - "36" % 5 + "2"
        console.log(c)
Copy the code

Output:

20
1
22
Copy the code
  • Implicit conversion: In addition to the string in + operation, the other cases, all other data types involved in mathematics, computer secretly will automatically be converted to other data types to numeric type, then participate in operation, the process does not need to use the parseInt (), Number () method, such as process is secretly, this is an implicit conversion process.

Implicit conversion

Other data types are implicitly converted to numeric types:

  • Corresponding digit: The string of pure digits is converted to the corresponding digit 123 →123
  • Convert to 1: true
  • Converts to 0: false, null, “” empty string, blank string
  • Convert to NaN: undefined, non-empty, non-pure numeric string

Comparison operator

Also called relational operators. A comparison operator compares its operands and returns a Boolean value. The result is either true or false.

> Greater than < less than >= Greater than or equal to <= Less than or equal to == Equal. === congruent === congruent === congruent === congruent === congruent === congruent === congruent == incongruent, as opposed to full equalityCopy the code

Abnormal case 1: Special value participates in comparison operation

  • NaN participation: unequal and unequal results are true, everything else gets false
  • Infinity operations, depending on the situation, such as:
Infinity == Infinity ->True
Infinity === Infinity ->True
Infinity > Infinity ->False
Infinity >= Infinity ->True
Copy the code

Abnormal case 2: Comparison operations involving other data types (excluding string to string comparisons)

Other data types are also implicitly converted to numbers for comparison.

"123" →123 True →1 false→0 NULL →0 undefined→NaN "" →0" ABC "→NaNCopy the code

For null and 0, equality is false, >= and <= are true

null == undefined -> True

Abnormal case 3: String to string comparison

Will not occur implicitly converted to digital, but compares two strings of Unicode encoding sequence Character encoding sequence: once upon A time in the future 0-9, a-z, a-z, when they are compared to less than in front of the back of the don’t care about the length of the two strings, starting from the first character comparison, comparison, in turn, will be back, until the size, is no longer in the future

Logical operator

Logical operators are commonly used between Boolean values; When the operands are Boolean, the return value is also Boolean

&& logic and operator and | | the Boolean or operator. Logical non-operatorsCopy the code

Abnormal condition

  • In addition to Boolean values, values of other data types can also participate in logical operations. Operands need to be implicitly converted to Boolean values to participate in the judgment calculation, and the final calculation result is still the original location of the data.
  • Not all logical operations return booleans; other data is the data itself

Implicit conversion to Boolean values

  • False: NaN, 0, “” empty string, null, undefined
  • Conversion to true: non-0, non-nan number, non-empty string

When they are used for non-Boolean values, the return value may be non-Boolean. The calculation is actually quite simple:

  • If a can be converted to false, return a; Otherwise, return b
  • If a (logic or a | | b) can be converted to true, then the return a; Otherwise, return b

Logical operator operation order

Comprehensive operation order: not, and, or

The assignment operator

= is equal to += is equal to -= is equal to *= is equal to /= is equal to %= Mod is equal to ++ increments -- decrementCopy the code

Unary operator

+ + - -!Copy the code

++ or — symbols can be written before and after variables, which may result in different results

Take ++ for example:

  • A ++: ++ symbol after the variable, a++ is used in the process of participating in the program did not increment the original value, after the second use of a variable, A uses the new value of the increment. Participate first, then add.
  • ++a: ++ symbol before the variable, ++ A in the process of participation in the whole use of a plus 1 after the new value, after the second use of a variable, A also uses the new value of 1. First add, then participate

Case 1

var a = 3; 
var b = a++; 
var c = ++a;
console.log(a,b,c)
Copy the code

Output:

5 3 5
Copy the code

Case 2

Output:

Operational priority

Priority from high to bottom:

2. Unary operator ++ --! 4. Relational operator > >= < <= 5. The equality operator ==! = = = =! = = 6. Logical operators && before | | 7. The assignment operatorCopy the code

case

var a = 4; var num = 1 * (2 + 3) && a++ || 5 > 6 && 7 < 8 || ! 9. console.log(num)Copy the code

Output:

4
Copy the code