Habushu
September 20, 2022, 1:47am
1
Tell us what’s happening:
Hi there,
I have a bit question here what’s the difference between if (arr[i])
and if(arr[i] === true
)?
**Your code so far**
function bouncer(arr) {
let newone =[];
for (let i = 0; i <arr.length; i++){
if(arr[i] === true) {
newone.push(arr[i]);
}
}
return newone;
}
console.log(bouncer([7, "ate", "", false, 9]));
when i run this code, it won’t work. but as soon as i delete === true
it will work just fine. I wonder why?
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36
Challenge: Basic Algorithm Scripting - Falsy Bouncer
Link to the challenge:
When you type if (arr[i] === true)
you are checking that element in the array if it is the boolean true
, if its not the boolean true
then it is false
. But if you type if (arr[i])
its seeing truthy values and saying true
and running your code. Anything that is not a falsy value is truthy.
In JavaScript, a truthy value is a value that is considered true when encountered in a Boolean context. All values are truthy unless they are defined as falsy. That is, all values are truthy except false, 0, -0, 0n, "", null, undefined, and NaN.
1 Like
Habushu
September 20, 2022, 2:12am
3
I see thanks! I’ll spend more time exploring MDN
system
Closed
March 21, 2023, 3:21pm
5
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.