Number() function
The Number() function performs conversions based on these rules:
Parameter | Number Function Returns |
---|---|
Boolean true | 1 |
Boolean false | 0 |
null | 0 |
undefined | NaN |
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 |
objects | the 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>
Home
JavaScript Book
Language Basics
JavaScript Book
Language Basics
Data Types:
- JavaScript Data Types
- typeof Operator
- The Undefined Type
- null Type
- null vs undefined
- Boolean Type
- Boolean() casting function
- The Literials of Number Type
- Octal Integer
- Hexadecimal
- Floating-Point Values
- Value range
- NaN
- Number Conversions:Number(), parseInt() and parseFloat()
- Number() function
- parseInt()
- parseFloat()
- The String Type
- String Literals and Escapes
- Get the String Length
- Converting to a String with toString() method
- Convert Number to String with radix
- Convert to String with String() casting function