Number of Detailed data type JS

Number data type

Number type uses IEEE754 format for representing integers and floating-point values, which is 0.2 + 0.3not equal to 0.5the cause,

The most basic type of the value is a decimal integer literal format

var a = 10;

1. floating-point values

2. In

Not a Number: Is not a number, but it is numeric

NaN features

  • Any NaN values ​​and operations return NaN
  • NaN is not equal to any value, including itself NaN
console.log(NaN == NaN)

3. isNaM function

Not for detecting a variable value ( Number) type. isNaN()Receiving a parameter, this parameter may be any type, this function will try to convert the incoming parameter values, some values may not be directly converted into numeric values, such as string "10"or Booleanvalue is not converted into a numerical value returned true, You can convert returnfalse

console.log( isNaN(NaN ) // true 
console.log( isNaN(10 ) // false
console.log( isNaN('10') // false
console.log( isNaN('blue') // true
console.log(isNaN( true )) // false (true会被转换成1)

to sum up:

  • isNaNMeans: 是不是非数值is then returned true, the function converts all can be Numbervalue types, returnsfalse
  • When using isNaN detecting, first verifies whether the detected value of the digital type, if not, to Number () This method is based on the value converted into digital type, then detects
  • Empty string, empty array, Boolean, nullwill turn into values, the isNaNfunction returnsfalse
  • Null object, functions, undefinedcan not be converted into a numerical value, returnstrue

4. Numerical conversion

The non-numeric values ​​to numeric values:

  • Number()
  • the parseInt () rounding
  • parseFloat()

Number () conversion rules:

  • If it is a Boolean value, trueand falsewill be converted to 1and0
Number(true) // 1
Number(false) // 0
  • If it is digital, but simply pass and return
Number(1) // 1
Number(100) // 100
  • If the value is null, 0 is returned
Number(null) // 0
  • If it is undefined, NaN is returned
Number(undefined) // NaN
  • If a string:
    • String contains only numbers (including government)

Guess you like

Origin www.cnblogs.com/dobeco/p/11619100.html
Recommended