When to use is NaN, parseFloat & parseInt

ABU HASAN
2 min readMar 3, 2023

--

1. isNaN() Function

The number type in JavaScript is a set of all number values, including “Not A Number”, positive infinity and negative infinity. “Not a Number” is a value that does not represent a real number, despite having number type. NaN is useful to represent faulty operations on numbers.

Example

isNaN('Hello') //true
isNaN('2005/12/12') //true
isNaN(undefined) //true
isNaN(‘NaN’) //true
isNaN(NaN) //true
isNaN(0 / 0) //true
isNaN(123) //false
isNaN(-1.23) //false
isNaN(5–2) //false
isNaN(0) //false
isNaN('123') //false
isNaN('') //false
isNaN(true) //false
isNaN(null) //false

2. parseFloat() Function

The parseFloat() method parses a string and returns its value as a floating-point number. If the parseFloat() method is passed a value that can not be converted to a floating-point number, it will return NaN.

Example

  var a = parseFloat("10") //10
var b = parseFloat("10.00") //10
var c = parseFloat("10.33") //10.33
var d = parseFloat("34 45 66") //34
var e = parseFloat(" 60 ") //60
var f = parseFloat("40 years") //40
var g = parseFloat("He was 40") //NaN

3. parseInt() Function

The parseInt() function is used to accept the string ,radix parameter and convert it into an integer. The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.

Exemple

  var a = parseInt("10") //10
var b = parseInt("10.00")//10
var c = parseInt("10.33") //10
var d = parseInt("34 45 66") //34
var e = parseInt(" 60 ") //60
var f = parseInt("40 years") //40
var g = parseInt("He was 40") //NaN
var h = parseInt("10", 10) //10
var i = parseInt("010") //10
var j = parseInt("10", 8)//8
var k = parseInt("0x10") //16
var l = parseInt("10", 16) //16

--

--

ABU HASAN
ABU HASAN

No responses yet