Why is isNaN ([3]) false in JavaScript?

I am using Chrome 60.x browser and checking the code isNaN([3]). The result false, but I can’t understand it.

[3]is an array and it is not empty. I think that [3]this is an array object and it is not a number.

Otherwise, the result isNaN(["ABC"])is equal true. And one more result isNaN([1,2,3])- true. Therefore, I assume that the javascript engine is an array that changes strength by a number in which the array has one element.

Please let me know what happened. isNaNfunction for array parameter.

ref1: Why isNaN (null) == false in JS?
ref2: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN


[EDIT] Thank you all for your reply.

I understand that javascript parsed the value implicitly before the comparison.

And I found a useful link reading Nina Scholz's answer.
Comparison table: http://dorey.imtqy.com/JavaScript-Equality-Table/

+4
source share
5 answers

When you use it isNaN, it tries to parse your input value into number, and then checks if it is there NaNor not.

See some examples.

For an array with one element, the function Number()returns an object that actually holds the first element as a value (see console.log). For many elements, it returns NaN, so you get the result isNaNtrue.

const a = new Number([3]);
console.log(`a is ${a}`);
console.log(`isNaN(a) - ${isNaN(a)}`);


const b = new Number([3,4,5]);
console.log(`b is ${b}`);
console.log(`isNaN(b) - ${isNaN(b)}`);
Hide result
+5

, , toString, .

console.log(isNaN([3]));
console.log(isNaN('3')); // convert with toString to a primitive
console.log(isNaN(3));   // convert to number
Hide result
+3

Javascript : integer, , . ,

x = increment([3])

x = 4.

isNan([3])

false, [3] 3, 3 - . , ["ABC"] integer, isNaN(["ABC"]) = true. , javascript [1,2,3] , ,

isNaN([1,2,3]) = true
+2

, Javascript, , , , . ==

3 == [3] // returns true

, [0] , [n] n , , .

[0] == false // returns true 
[1] == false // returns false
+1

, , isNaN([3])//returns false

Number([3]) === [3] //returns false 
Number(3) === 3 // returns true 

, ===

0

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


All Articles