Why parseInt fails to validate arrays correctly in Javascript?

Given:

var obj={0:21,1:22}

and

var arr=[21,22]

Why parseInt(obj)returns NaN, but parseInt(arr)returns 21?

I had a function in which I was going to pass int, a hash type objector a simple one array. I expected to parseIntreturn NaNfor objectand array, which will simplify the verification of arguments. What gives?

+4
source share
1 answer

, parseInt . String(obj) "[object Object]" , String([21,23]) "21,23", parseInt , char.

parseInt:

inputString be? ToString (string).

( ).

S , radix-R, Z - S, ; Z - S.

( , , "21,23""21").

+8

Source: https://habr.com/ru/post/1669891/


All Articles