Global JavaScript Methods

Global JS Methods

JavaScript global methods can be used on all JavaScript data types.

  • Number ( )
  • parseFloat ( )
  • parseInt ( )

Number ( )

The Number() function converts the object argument to a number that
represents the object’s value.
If the value cannot be converted to a legal number, NaN is returned.
If the parameter is a Date object, the Number() function returns the number of
milliseconds since midnight January 1, 1970, UTC.

Ex: –

Number(true)
Number(“100”)
Number(100/“Hello”)

parseInt ( )

The parseInt() function parses a string and returns an integer.

Syntax:- parseInt(string, radix)

The radix parameter is used to specify which numeral system to be used, for example, a
the radix of 16 (hexadecimal) indicates that the number in the string should be parsed from
a hexadecimal number to a decimal number.

If the radix parameter is omitted, JavaScript assumes the following:

  • If the string begins with “0x”, the radix is 16 (hexadecimal)
  • If the string begins with any other value, the radix is 10 (decimal)

Only the first number in the string is returned.
Leading and trailing spaces are allowed.
If the first character cannot be converted to a number, parseInt() returns NaN.

parseInt ( )

parseFloat ( )

The parseFloat() function parses a string and returns a floating-point number. This function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string.

Syntax: – parseFloat(string)

  • Only the first number in the string is returned!
  • Leading and trailing spaces are allowed.
  • If the first character cannot be converted to a number, parseFloat() returns NaN.

parseFloat ( )

Tagged : / / / / /
0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x