'Check for falsy values on array
I don't understand why this piece of code works can someone explain to me?
If I delete this piece of the conditional && arr[i] the value of arr[5] don't assume as a falsy value, but if I write that piece of code already assumes arr[5] like a falsy value.
You can see the value of arr[5] on the end of the function.
function bouncer(arr) {
let word = []
for (let i = 0; i < arr.length; i++)
if (typeof arr[i] !== Boolean && arr[i]) {
word.push(arr[i])
}
return word;
}
console.log(bouncer([false, null, 0, NaN, undefined, ""]));
Solution 1:[1]
maybe You can do it simplier :-)
let arr = [false, null, 0, NaN, undefined, "", 111, "aaa"];
let x = arr.filter(i => (typeof i !== Boolean && i))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |

