Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use Number.parseInt instead of parseInt()?

From documentation:

This method behaves identically to the global function parseInt()

But, as it's experimental, the compatibility is worst. For example, is not available in IE or Safari.

So, why should a developer use Number.parseInt()?

like image 772
Mario Levrero Avatar asked Feb 05 '15 17:02

Mario Levrero


People also ask

What is the difference between parseInt and number?

This example shows the difference that parseInt() only returns an integer value whereas Number() returns all the digits including floating points.

What happens if you parseInt a number?

The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN . If not NaN , the return value will be the integer that is the first argument taken as a number in the specified radix .

What is the purpose of the parseInt ()?

The parseInt() function is used to accept the string ,radix parameter and convert it into an integer. The radix parameter is used to specify which numeral system to be used, for example, a radix of 16 (hexadecimal) indicates that the number in the string should be parsed from a hexadecimal number to a decimal number.


1 Answers

The use of Number.parseInt being encourage overparseInt() is due to a trend in the JavaScript community of get away from using globals. The Mozilla documentation on Number.parseInt states:

...and is part of ECMAScript 2015 (its purpose is modularization of globals).

There you have it. It's because of global-phobia :)

like image 100
Evan Wieland Avatar answered Sep 20 '22 05:09

Evan Wieland