JS type conversion (display, implicit)

1. Type conversion (display, implicit)

Display type conversion:

1. parseInt to integer

var a = '123.1px';
console.log(parseInt(a)); //123 converts a string to an integer, taking only the first integer

2. Convert parseFloat to floating point number (decimal)

var a = '123.2';
console.log(parseFloat(a));//123.2

3. toString to string

Other types are spliced with strings, then the spliced type is string type

var a = 123;
var b = c.toString();
console.log(typeof b);

Implicit type conversion (no conversion by other means)

1. Both sides are numeric strings, with operators connected

console.log(3 * '8'); //24
console.log('3' * '8'); //24
​
console.log(3 * null);// null will be converted to 0
console.log(3 * false);//false to 0
console.log(3 * true);//true to 1
​
console.log(3 * '9px');//NaN is not a number. Impure numbers cannot be implicitly typed
console.log(3 * undefind);//NaN ubdefind can't implicitly type cast

Type conversion summary:

No matter what kind of operation, as long as undefined is involved in the operation, the result is NaN;

String, false, true and null of numeric type can be implicitly converted, except for plus sign;

The plus sign is quite special, so implicit type conversion is impossible, except for Boolean value;

2. Operator

1. Mathematical operators

/*  + - * / % ()  */

2. Relational operator

> < >= <= == != ===(Congruence) !==(Incongruence)
//Boolean value will be returned eventually
​
var a = function(){
    alert();
}
var b = function(){
    alert();
}
console.log(a==b); //false
​
/* === Congruence:
Basic data types compare data as well as values; types 
Reference type compare memory address
*/
console.log(11=='11'); //true
console.log(11==='11');//false

3. Logical operator

/*
&&Logic and truth are true, false a if there is false & b a true throw b a false throw a
 ||Logic or truth is true, all false is false a|b a true throw a a false throw B
 ! Logical non
*/
​

4. Assignment operator

/* + += -= ++ -- /= %/ *=*/
var a = 11;
a+=10;
console.log(a);
​
var b = 12;
console.log(b--);
console.log(b++);
​
/*
    ++Differences before and after variables
    (Participate in operation) + + a add first and then execute
*/
var x = 10;
var y = x++;
console.log(x);//11
console.log(y);//10
​
var z = 2 + x++;
console.log(x);//11
console.log(z);//12

3. Operator practice

/*
Final result of the job: summarize the calculation order (priority) of the operator
   var a = 3 < 6 && 7 < 14;    //true
        Original = true & & true
        = true

    var a = 1 + 2 < 3 + 3 && 3 + 4 < 2 * 7;
        Original formula = 3 < 6 & & 7 < 14
        = Last question
        = true

    var a = false + true && 13;
        Original = 0 + 1 & & 13
        = 1 && 13
        = 13


    var a = 15;
    false + a++ + true > 8 && 13 || 6
        Original formula = false + 15 + true > 8 & & 13 | 6
        = 16 > 8 && 13 || 6
        = true && 13 || 6
        = 13 || 6
        = 13


*/
var a = 3 < 6 && 7 < 14;  //true

var a = 1 + 2 < 3 + 3 && 3 + 4 < 2 * 7;//true

var a = false + true && 13;//13

var a = 15;
false + a++ + true > 8 && 13 || 6 //13

​
       

 

Posted by vince251 on Wed, 20 Nov 2019 06:56:07 -0800