Number() function

The Number() function performs conversions based on these rules:

ParameterNumber Function Returns
Boolean true1
Boolean false0
null0
undefinedNaN
Empty string("")0
String with only number, for example "123"123
String with only number and plus sign, for example "+123"123
String with only number and minus sign, for example "-123"-123
Leading zeros are ignored, for example "0123"123
Leading zeros are ignored, for example "+0123"123
Leading zeros are ignored, for example "-0123"-123
String with valid floating-point format, such as "1.1"1.1
String with valid floating-point format, such as "+1.1"1.1
String with valid floating-point format, such as "-1.1"-1.1
Leading zeros are ignored, such as "01.1"1.1
String with hexadecimal format, "0xf"15
String with hexadecimal format, "-0xf"-15
String with not a number value, for example "asdf"NaN
objectsthe valueOf() method is called and the returned value is converted
 
<!DOCTYPE html>
<html>
<head>
    <title>Number Example</title>
    <script type="text/javascript">
        document.writeln(Number("-123"));   //-123
        document.writeln(Number("+123"));   //123
        document.writeln(Number("-0123"));  //-123
        document.writeln(Number("0xf"));    //15
        document.writeln(Number("-0xf"));   //-15
        document.writeln(Number("asdf"));   // NaN
    </script>
</head>
<body>
</body>
</html>
  
Click to view the demo
Home 
  JavaScript Book 
    Language Basics  

Data Types:
  1. JavaScript Data Types
  2. typeof Operator
  3. The Undefined Type
  4. null Type
  5. null vs undefined
  6. Boolean Type
  7. Boolean() casting function
  8. The Literials of Number Type
  9. Octal Integer
  10. Hexadecimal
  11. Floating-Point Values
  12. Value range
  13. NaN
  14. Number Conversions:Number(), parseInt() and parseFloat()
  15. Number() function
  16. parseInt()
  17. parseFloat()
  18. The String Type
  19. String Literals and Escapes
  20. Get the String Length
  21. Converting to a String with toString() method
  22. Convert Number to String with radix
  23. Convert to String with String() casting function