javaScript base with Number () to convert other types of type Number

A: basic types

String

The string into a number, as long as a non-string contains any valid numeric character (except the first point) the result is NaN, the empty string becomes zero digit

console.log(Number("12.5")); //12.5
console.log(Number("12.5px")); //NAN
console.log(Number("12.5.5px"));//NAN
console.log(Number(""));//0

Boolean

the console.log (Number The ( to true )); // . 1 
the console.log (Number The ( to false )); // 0 
the console.log (isNaN ( to false )); // to false is a valid number

null和undefined

console.log(Number(null));//0
console.log(Number(undefined));//NaN

II: reference data types

Converting a digital reference data types that it is based on first toString () is converted to a string, and then converted to digital

the console.log (Number The ({NUM: "10"})); // NaN3 
the console.log (Number The ({})); // NaN3 ({NUM: "10"}). toString (); the "[ object Object] "non-numeric characters and so is effective NaN3 
the console.log (number The ([])); // 0 [] .toString () is" "so to digital is 0 
the console.log (number The ([12 is]) ); // 12 [12] .toString () is "12" is therefore converted to a digital 12 
the console.log (number The ([12,23])); // NaN3 [12] .toString () is "12, 23 "inside" and "non-numeric characters so effective is NaN

Related interview questions

A = 10 + the let null + to true + [] + + undefined 'Tencent' + null + [] + 10+ to false ; 
the console.log (A) // 11undefined Tencent null10false
becomes null number is 0, true is 1, [] goes digital, first experience becomes empty string, the string encounter, Han did not even think about, directly into a string concatenation.
When removed from the front of undefined [] results becomes NaN Tencent null10false

Guess you like

Origin www.cnblogs.com/zimengxiyu/p/11666517.html