+ mathematical operations and character concatenation

  1. When you have strings in an expression, it's string concatenation otherwise it's math
  2. Mathematical operations:Anything that is not a string is converted to a number first
  3. String concatenation:Anything that is not a string is converted to a string by calling toString first
  4. inObject data typeIn mathematicsIn addition to ordinary objectsEverything elseGo first stringagainString concatenation
  • 1+true => 2 Math operation
  • ‘1’ + true => ‘1true’ string concatenation
  • [12] + 10 => ‘1210’ though nowNo strings, butReference data typetodigital.It's a string first, so it becomes string concatenation,(Except for ordinary objects, all other object data types are converted to strings and then concatenated)
  • ({}) + 10 => “[object Object]10”
  • []+ 10 => “+ 10 => ’10’The output is direct from the console. Console. log results in "[object object]10".
  • {} +10 => 10 This doesn’t matter because it’s neither math nor string concatenation, it’s two-part code
    • {} represents a code block (block-level scope)
    • Plus 10 is our operation
    • Strictly written: {}; + 10; The result is 10
    • Function () {} +10 => +10; = > 10
    • + 10 is 0 + 10
12+true+false+null+undefined+ + []'Hahaha'+null+undefined+ + []true

12+true= >13
13+false= >13
13+null= >13
13+undefined= >NaN
NaN+ [] = >NaN+' '= >'NaN'
'NaN'+'Hahaha'= >'NaN ha ha ha '
'NaN ha ha ha '+null+undefined= >  'NaN ha ha nullundefined'
'NaN ha ha nullundefined'+ [] = >'NaN ha ha nullundefined'+' '= >'NaN ha ha nullundefined'

'NaN ha ha nullundefined'+ true= >  'NaN ha ha nullunDefinedTrue '
 
Copy the code

== If the data types on the left and right sides are different, they are converted to the same type (numeric type) before comparison

Object == object: not necessarily equal, objects operate on reference addresses, different addresses are not equal

{name:'aa'} = = {name:'aa'} = >false[] [] = =false

var obj={}
var obj1=obj,obj2=obj;
obj1==obj2   true

Copy the code

Different data types are compared by converting other values to numbers and then comparing them

Object == number: Convert objects to numbers and compare

Object == Boolean: Converts objects to numbers and booleans to numbers

Object == string: Converts objects to numbers and strings to numbers

String == number: Converts a string to a number

String == Boolean: all converted to numbers

Number == Boolean: Boolean converts to number

[] = =! []: true

! []! The symbol is converted to Boolean [] ==falseThe == operator compares different types of numbers first0= =0
true

//typeof [] //Object
//typeof ! [] //Boolean
Copy the code

Null == undefined :true; Js specifies that null and undefined are equal in the equality operator.

Null does not equal any data except undefined and itself.

Null and undefined cannot be converted to any other data type until equality is compared (i.e., null is not equal to any data except undefined, although Number(null) results in 0 but 0 == null is false)

null === undefined :false

=== null, undefined, and other values are not equal, only equal to themselves

null == null true

undefined == undefined true

NaN== NaN false

NaN is not equal to anyone including herself

1= =true    :true
1= =false   :false
2= =true    :falseHere is thetrueChange to number [] == [] :false[] = =false   :trueIt all goes into numbers0= =0[] = =true    :falseIt all goes into numbers0= =1! [] = =false  :true   / / to be first! [], convert the array to a Boolean to get true => false == false

Copy the code

The equal sign operation

= : assign, the left side of the equals sign is the variable, the right side is the value; (Whatever is on the right is converted to the value first)

== : compare, if the left and right values are not the same type, the browser will default to the same (converted to a number) for comparison; For example: ‘6’ ==6 => ‘6’ ->6 => 6==6 =>true

=== : absolute equality, not only requires the same value, and the type must be exactly the same; ‘6’===6 false

&& logic and VS | | logic or

1. In the judgment of conditions

When all conditions are true, the whole is true

| | : as long as there is a condition is true, the whole is true

2. In assignment

| | : A | | B first to see the true and false, A true returns A value, A returns the value of B is false (no matter what is B) 1 | | = 2 > 1; 0|| false=>false;

If A is true, the value of B is returned. If A is false, the value of A is returned. 1 & 2 = > 2; 0&&false => 0

In real projects we useLogic orImplements the default setting operation, assigning the default value of the parameter

callback && callback(); Callback executes if callback is true

Logic andIs of higher priority thanLogic orTo calculateLogic and

function fn(num,callback){
    //=> if num does not pass a value that defaults to 0;
    //if(typeof num === 'undefined') num=0; More rigorous
    num=num || 0;

    // If the callback passes a function, the function is executed
     //if(typeof callback === 'function') callback(); More rigorous
     callback && callback();
}
Copy the code
0 || 2 && false || 3

2&&false   //false
0||false   //false
false || 3 / / 3
Copy the code

Math operations: +, -, *, /, %,

+ is not just math, but maybe string concatenation

I++ is pure math and has abandoned the rule of string concatenation

The math first converts a non-numeric call to Number()

'3' - 1= >2
	Number('3') = >3
	3-1= >2

'3px' - 1= >NaN

'3px' + 1= >'3px1'

var  i=3;
i+='1'; = >'and'
i=i+'1'; = >'and'i++; = >4  //i++ is pure math and has discarded string concatenation rules
Copy the code

I++ and + + I

I ++: takes the original value of I and other values to calculate, and then accumulates itself after the calculation is complete

++ I: add 1 by itself, and then calculate the sum with other values

var i=5;
console.log( 10 + (++i) + (i++) + 5 + (i++) + (++i))
             10 +  6  + 6   + 5 + 7 +  9
                  I=6  I=7      I=8  I=9
Copy the code
var i=5;
console.log(5+i++)    =>10   5Plus (i++), parentheses are the same thing1
console.log(i) => 6

var i=5
console.log(5+ + + I) = > an errorconsole.log(5+ ++i) => 11
console.log(5+ (++i)) => 11
Copy the code