# Detailed explanation of the difference case between JavaScript parseInt of and Number of

• 2021-11-14 04:40:45
• OfStack

## Learning objectives:

parseInt () and Number () are used most often to convert a string to a data type, so what are the differences between them?

## Learning content:

The parseInt () function resolves the given string to an integer at the specified cardinality.
The second parameter indicates the binary system used. We generally use decimal, but we may also use 8 or hexadecimal. To avoid parsing errors for strings beginning with "0" and "0x", various javascript programming specifications stipulate that the value of the second parameter must be explicitly given, such as parseInt ("123", 10).

``````
parseInt('16', 8)  = 14
parseInt('10', 8)  = 8

parseInt('16', 10)  = 16
parseInt('10', 10)  = 10

parseInt('16', 16)  = 22
parseInt('10', 16)  = 16``````

parseInt parses string from scratch as an integer, and returns the parsed integer part when it encounters unparsed characters. If the first character cannot be parsed, it directly returns NaN.

Number () can be used to perform type conversion when the new operator is not used. If it cannot be converted to a number, NaN is returned. Like "123a", parseInt () returns 123 and Number () returns NaN, different types of strings use the conversion difference of these two functions:

``````
//  When a string is composed of numbers,   The numbers they convert 1 There is no difference between the samples
let numStr = '123'
console.log(parseInt(numStr))   //123
console.log(Number(numStr))		//123

//  When a string is composed of letters,
let numStr = 'abc'
console.log(parseInt(numStr))   //NaN
console.log(Number(numStr))		//NaN

//  When a string is composed of numbers and letters,
let numStr = '123a'
console.log(parseInt(numStr))   //123
console.log(Number(numStr))		//NaN

//  When the string is defined by the 0 And numbers
let numStr = '0123'
console.log(parseInt(numStr))   //123
console.log(Number(numStr))		//123

// ** When a string contains a decimal point **
let numStr = '123.456'
console.log(parseInt(numStr))		//123
console.log(Number(numStr))			//123.456

// ** When the string is null Hour **
let numStr = null
console.log(parseInt(numStr))		//NaN
console.log(Number(numStr))			//0

// ** When the string is ''( Empty ) Hour **
let numStr = ''
console.log(parseInt(numStr))		//NaN
console.log(Number(numStr))			//0

``````

Learning summary:

1. When the string is composed of numbers, there is no difference in the number 1 they convert; If the string contains no numbers and is all letters, both methods simply return NaN results; When the string is composed of 0 and numbers, all numbers except 0 are parsed;

2 When the string is composed of numbers and letters, the letter is at the beginning, and both methods only return NaN results. ② The letter is not at the beginning. Number method returns NaN, and pareseInt method returns data before the letter

3 parseInt For non-String values, first convert to String type and then operate 4 For the remaining details, refer to the above case

Related articles: