A bit of question on Basic Algorithm Scripting - Falsy Bouncer

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.

1 Like

I see thanks! I’ll spend more time exploring MDN

Very nice explanation!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.