Lapys
Lapys

Reputation: 946

What is the critical difference between 'Number.parseInt()', 'Number.parseFloat()', 'Number()' or '+'?

The basic question here is how do I know when to use and

what is the critical difference between each of them:

for converting JavaScript values (mostly String's) to numbers.

Especially since all of them give similar values and can convert String to its Number representation:

Number.parseInt("2")   // returns 2
Number.parseFloat("2") // returns 2
Number("2")            // returns 2
+"2"                   // returns 2

/* Plus a few more methods... */
eval("2")              // returns 2
JSON.parse("2")        // returns 2

Upvotes: 4

Views: 3776

Answers (2)

Heinrich Henning
Heinrich Henning

Reputation: 933

Number.parseInt() calls the global function parseInt() in the background, same with Number.parseFloat() see: Number.parseInt ECMA and Number.parseFloat ECMA

The calls Number("2") and "+2" is identical in the background, they both call ToNumber see: Number and Unary + Operator

When you know what types you are working with, or want a guaranteed type back, use parseFloat and parseInt, otherwise it tends to be easier to only use Number() as it will work within all your calculations, many people choose to use the unary + operator because they find it more pleasing to read/type, but that is only based on preference as it is identical to Number().

Also, when you using parseInt(), you can specify a radix, which is useful in certain applications where you want to work in different number systems, which you cannot do with Number()

If the ECMA standard references does not explain the details for you enough, I will add a summary for you.

Upvotes: 4

llama
llama

Reputation: 2537

  • Number.parseInt method (or just parseInt)

    • Ignores leading and trailing whitespace
    • Parses a leading number to an integer (not a floating point number)
    • Ignores invalid trailing data
    • Lets you set the base to use when interpreting the number
    • Will interpret text starting with 0x as hexadecimal, if another base was not provided
    • Returns NaN if the value could not be successfully parsed to an integer
  • Number.parseFloat method (or just parseFloat)

    • Similar to parseInt, except that it allows for a decimal part to be interpreted
    • Only parses to base-10
  • Number() function (or class?)

    • Similar to parseFloat, but does not allow trailing text
    • Will return 0 for an empty string or a string that only contains whitespace
    • It's not a class; when called without new, it returns a primitive number
  • the + operator

    • Basically the same as Number(), but in operator form.
  • eval()

    • Interprets and executes the given input as a JavaScript program.
    • Given the string "2", it will be interpreted as a numeric literal, and return that value since it's the result of the last expression in the program
    • Throws an error if the input was not a valid program.
  • JSON.parse()

    • Parses the textual data as JSON-serialized data.
    • If the data is valid, it creates the JavaScript objects/primitives that are represented by the data, and returns them.
    • If the data is invalid, it throws an error.
    • Given the string "2", it will be interpreted as a numeric literal, and return the value that was successfully parsed out of it according to the parsing requirements of JSON.

So you decide which is appropriate to use based on their capabilities.

Upvotes: 10

Related Questions