+ 1

isNaN

Is type coercion the reason why empty strings and empty arrays are considered numbers? Or is the code below more of an instance of Truthy Falsy.  let input5 = ""; if (!isNaN(input5)) {     document.write("a"); } else {     document.write("b") } // output is a let input6 = []; if (!isNaN(input6)) {     document.write("a"); } else {     document.write("b") }    // output is a let testZero = 0 let testOne = "12"; let testTwo = ""; let testThree = []; let testFour; let testFive = true; let testSix = null; document.write(isNaN(testZero)); // false document.write(isNaN(testOne)) // false document.write(isNaN(testTwo)); // false document.write(isNaN(testThree)); // false document.write(isNaN(testFour)); // true document.write(isNaN(testFive)); // false document.write(isNaN(testSix)); // false P.S. I expected test Five to be true because I read true is considered to be 1. Thank you!

3rd Dec 2018, 6:16 AM
DumbledoresAmy
DumbledoresAmy - avatar
2 Réponses
0
Thank you for the reply. I know that NaN is Not a Number. I'm unclear why empty strings and empty arrays are seemingly translated as numbers. I know that empty strings are falsy. But, empty arrays are objects and so they are truthy. So, it surprised me that both values return as False with NaN. My best guess is something to do with type coercion but I feel like I'm missing something.
3rd Dec 2018, 7:28 AM
DumbledoresAmy
DumbledoresAmy - avatar
0
During my lunch time dig for information I found that both empty strings and empty arrays are translated as 0. 0 also returns false with isNaN. Links to follow after work 😄
3rd Dec 2018, 8:52 PM
DumbledoresAmy
DumbledoresAmy - avatar