The Difference between Number and parseInt in JavaScript

Web Dev
Sponsored links

In this article, I will introduce the difference between Number and parseInt in JavaScript.

What is Number in JavaScript?

Number is one of the objects and is an operation to convert an argument such as a character string or a number to a Number type.

As shown below, if you pass the string "123" as an argument, it will be converted to the number 123.

Number('123')  // returns the number 123
Number('123') === 123  // true

Number("plantpot")  // NaN
Number(undefined)  // NaN

If you pass a value such as "abc" as an argument, the return value "NaN" is returned because it is not a number, which means that it cannot be converted.

What is parseInt in JavaScript?

An object whose arguments can be parsed to get an integer.

Let's set the number string you want to convert to the argument.

let int = parseInt("123") // 123

Although parseInt is often used like parseInt("123"), it can be used in the following code.

parseInt(string, radix)

For example, when dealing with integers in decimal, write as follows.

parseInt("123", 10);