Type conversion in JS

Posted by maxime on Fri, 01 May 2020 09:02:23 +0200

Because the data type in JS is loose type, and it does not detect the data type first like C + + and Java when performing operator operation, but implicitly performs data type conversion. Sometimes the return value is not as we think, so we need to understand the display and implicit type conversion in JS, so as to make better use of it in engineering

1. Display type conversion

Number() to convert things into numeric types

Number('123');          //123
Number('-123');         //-123
Number('a');            //NaN
Number(true);           //1
Number(null);           //0
Number(undefined);      //NaN
Number(NaN);            //NaN

ParseInt (string, radius), which converts a string to an integer in the corresponding base

parseInt('10', 16);     //16
parseInt('b', 16);      //11
parseInt('b');          //NaN
parseInt('3', 2);       //NaN, because 3 is not binary
parseInt(true);         //NaN
parseInt('123abc');     //123
parseInt('abc123');     //NaN

Parsefloat (string, radius), which converts a string to a floating-point number in the corresponding base


Convert things to string type, undefined and null can be converted to string form

String(null);           //"null"
String(undefined);      //"undefined"


Convert things to string type, undefined and null cannot be used, and radix is converted to target base based on decimal system

var num = 100;
num.toString();     // "100"
//Note that 123.toString() cannot be directly reported as an error

2. Implicit type conversion


To determine whether it is NaN, Number() will be called first and then compared with NaN after conversion

isNaN(123);         //false

isNaN("123");       //false, string
//Number('123')  ==> isNaN(123)  ==> false

isNaN("null");      //true, string
//Number("null")  ==> isNaN(NaN)  ==> true

isNaN(null);        //false, null object
//Number(null)  ==> isNaN(0)  ==> false

isNaN(undefined);   //true
//Number(undefined)  ==> isNaN(NaN)  ==> true

isNaN("123bcd");    //true
//Number("123bcd")  ==> isNaN(NaN)  ==> true

++/--+ / - (self increasing and self decreasing symbols, one yuan plus and minus)

var a = '123'; a ++;     //a 124
var b = 'abc'; a ++;     //b NaN; typeof(b) ==> number
var c = + 'abc';         //c NaN; typeof(c) ==> number

+Plus sign, when there is a string on both sides of the plus sign, String() will be called to turn both into strings

var a = 1 + "123";      //"1123"
var b = "abc" + 12;     //"abc12"

-*%(addition, subtraction, multiplication and division), Number() will be called to change both sides into number type

var a = 1/"2";      //a  0.5;typeof(a)  number

&&||! Convert to boolean value

>The implicit call Number() is converted to a number if there is a comparison between a string and a number

//Undefined, null, "ABC" is converted to NaN
undefined > 0       //false
undefined < 0       //false
undefined == 0      //false
null > 0            //false
null < 0            //false
null == 0           //false
"abc" > 0           //false
"abc" < 0           //false
"abc" == 0          //false

== !=

undefined == undefined //true
null == null           //true
NaN == NaN             //false

3. Without type conversion

=== !==

NaN === NaN  //false, a special case

Topics: Java