A little help needed (JavaScript)

Hello guys, I’m testing if elements in an array are truthy values or falsy ones. I have NaN inside the array being looped through but it keeps returning the NaN as a truthy value. This is the code: https://codepen.io/Tunjidev/pen/eYgJagE?editors=1111

=====https://codepen.io/Tunjidev/pen/eYgJagE?editors=1111====
I created 2 separate arrays both containing truthy and falsy values (line 1 & 2). If an element in any of the arrays is truthy, I push them to a new empty array called truthy (line 5) and if its falsy I push them to an empty array called falsy(line 4). In one of the arrays I created (line 2) it contains a NaN element but for some reason when I console.log the final output, NaN didn’t get pushed to the falsy array. What am I doing wrong pls? Thanks

1 Like

Hello @Adetunji

Nan === NaN will lead to false

check this one

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN#testing_against_nan

i know it confusing but since there many way lead to NaN then there no way to know if that NaN come from the same way

this how to get NaN

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN#description

hope that help and have a nice day :slight_smile:

1 Like