Mega Code Archive

 
Categories / JavaScript Tutorial / Number Data Type
 

Casting to Number

The Number() type cast works in a manner similar to parseInt() and parseFloat(). Except that it converts the entire value, not just part of it. parseInt() and parseFloat() only convert up to the first invalid character (in strings),so "4.5.6" becomes "4.5". Using the Number() type cast, "4.5.6". If a string value can be converted entirely, Number() decides whether to use parseInt() or parseFloat(). UsageResult Number(false)0 Number(true)1 Number(undefined)NaN Number(null)0 Number("5.5")5.5 Number("56")56 Number("5.6.7")NaN Number(new Object())NaN Number(100)100