Differences between javascriptparseInt AND Number FUNCTIONS _ javascript skills

Source: Internet
Author: User
In js, if you use parseInt (& quot; 08 & quot;), 8 is generally returned, but 0 is actually returned. but 8 is returned with Number (& quot; 08 & quot. however, parseInt ("08", 10) can return 8.

To clarify the differences between the two,

See the difference between parseInt and Number written by others:

ParseInt
Parses a string argument and returns an integer of the specified radix or base.
Core functions
Implementation version: Navigator 2.0: If the first character of the string specified in parseInt (string) cannot be converted to a number, returns "NaN" on Solaris and Irix and 0 on all other platforms. navigator 3.0, LiveWire 2.0: Returns "NaN" on all platforms if the first character of the string specified in parseInt (string) cannot be converted to a number.



Syntax
ParseInt (string, radix)
Parameters
String A string that represents the value you want to parse.
Radix (Optional) An integer that represents the radix of the return value.



Description
The parseInt function is a built-in JavaScript function.
The parseInt function parses its first argument, a string, and attempts to return an integer of the specified radix (base ). for example, a radix of 10 indicates to convert to a decimal number, 8 octal, 16 hexadecimal, and so on. for radixes above 10, the letters of the alphabet indicate numerals greater than 9. for example, for hexadecimal numbers (base 16), A through F are used.

If parseInt encounters a character that is not a numeral in the specified radix, it ignores it and all succeeding characters and returns the integer value parsed up to that point. parseInt truncates numbers to integer values.

If the radix is not specified or is specified as 0, JavaScript assumes the following:



If the input string begins with "0x", the radix is 16 (hexadecimal ).

If the input string begins with "0", the radix is eight (octal ).

If the input string begins with any other value, the radix is 10 (decimal ).
If the first character cannot be converted to a number, parseInt returns "NaN ".
For arithmetic purposes, the "NaN" value is not a number in any radix. you can call the isNaN function to determine if the result of parseInt is "NaN ". if "NaN" is passed on to arithmetic operations, the operation results will also be "NaN ".


Example
The following example all return 15:
ParseInt ("F", 16)
ParseInt ("17", 8)
ParseInt ("15", 10)
ParseInt (15.99, 10)
ParseInt ("FXX123", 16)
ParseInt ("1111", 2)
ParseInt ("15*3", 10) The following example all return "NaN ":

ParseInt ("Hello", 8)
ParseInt ("0x7", 10)
ParseInt ("FFF", 10) Even though the radix is specified differently, the following example all return 17 because the input string begins with "0x ".

ParseInt ("0x11", 16)
ParseInt ("0x11", 0)
ParseInt ("0x11 ")
-----------------------------------------------
-----------------------------------------------
Converts a specified object to a number.
Core functions
Implement Navigator 4.0 and Netscape Server 3.0

Syntax
Number (obj)
Parameters
Obj is an object.



Description
If the object is a Date object, Number will return the Number of milliseconds that have elapsed since January 1, January 1, 1970, the Greenwich Mean Time. After this Date, it is a positive Number, and the previous value is a negative Number.
If obj is a string without a Number, Number returns NaN.


Example
The following example converts a Date object to a numeric value:
SCRIPT
D = new Date ("December 17,199 5 03:24:00 ");
Document. write (Number (d) +"
");
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.