Casting to Number : Type Conversion : Number Data Type JAVASCRIPT TUTORIALS


JAVASCRIPT TUTORIALS » Number Data Type » Type Conversion »

 

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().











































Usage Result
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








HTML code for linking to this page:

Follow Navioo On Twitter

JAVASCRIPT TUTORIALS

 Navioo Number Data Type
» Type Conversion