If statement with single condition | if (0) | i can't understand it!

Hi, all

for a whole week and i ask question every day and see a lot of people that helped me to understand and figure the problem that i meet during learning code and this encourage me to ask more question about some of JavaScript concept that i can’t understand them.
So, Thanks for every one helped me and will help me to understand those problem or concept :heartbeat::heartbeat:

on of these concept that i just meet it now :
It’s if statement with single condition like this

if ([0]) {
console.log([0] === true) //false

i don’t care about console output i i just want to understand what the if condition meaning
and for live example (i’ll remove it after understand the concept) :

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  let newarr = [];
  for (let i = 0; i < arr.length; i++) {
    if (Boolean(arr[i])) {
  return newarr;

console.log(bouncer([7, "ate", "", false, 9]));

in above example i use Boolean(arr[i]) to convert all items in array to Boolean value, but if i remove Boolean method it’s do the same thing ? !! ?

can some one explain what happened (i’d be delightful if you explain on the live example) ?
Thanks :blush:

Note : Sorry if my question that maybe seems to be stupid or small question but, it’ll help me a lot to understand what happened

Heya dude, the issue with yout function is that you take the boolean value on your conditional statement if (Boolean(arr[i])), but as you can see, what you then push to newarr is the unmodified value of the array, on the line newarr.push(arr[i])

Edit: Also! If you meet an if statement with a single condition, it just means
if(this thing evaluates to true)

So if(true) it’s true, because true===true, like if(false) returns false

Hi, @gabrieljim
First thank you for helping me

So, you say:

if (5) mean if 5 true ? or to make it simple if 5 == 5 | 5 == true {So do this } is that right ?
but, when i want to do that on live example it’s represent empty result like this

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  let newarr = [];
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] == true) {
  return newarr;

console.log(bouncer([7, "ate", "", false, 9]));

Note : there is no problem with my function? it’s just take the items that “true” into the new array

You don’t even need the comparison. You can write:

if (arr[i]) {

It achieves the same goal.

First thank you for helping me

1-if (arr[i] == true) does't work 
2- if (arr[i]) work 

i need to know why ‘2’ work and 1 doesn’t work ??
i mean if arr[i] == true = (arr[i]) // why the first didn't work ?
thank you again

@eslam The second one works since the if statement for arr[i] value returns true if arr[i]is a truthy value, and will return false when it is a falsy value like “null, NaN and 0”.

@jenipha thank you for your replaying but, why the first one doesn’t work is that the same condition ?
we say hey if statement if the item we getting from arr = true then push this item to our new array called newarr ?

i just want to go deep and understand what behind [If statement with single condition if statement with single condition ]

thank you again and sorry if i tried someone with me

Any control statement in any programming language does not take “a condition”, it takes any expression (comparison, function execution, do-block, etc) that evaluates to a Boolean value.

JavaScript being a loosely typed language introduces the concept of truthiness and falseness for single direct values since there’s no compiler or warning to yell at you if you use anything but a Boolean value inside the if statement. Usually here’s how it goes:

  • Strings: always true unless empty
  • Arrays: always true
  • Objects: always true
  • Numbers: always true unless 0
  • Booleans: pretty obvious
  • Prototype instances: always true even if for example you do Boolean(new Number(0))
  • Null: false
  • NaN: false
  • Functions: always true

@eslam Only the if statement will validate whether it is true or false, when you provide a condition inside it will test whether the condition is satisfied whereas when a single value is provided, it will test if that value is a truthy or falsy value.

I hope that clears your doubt

First Thank you for helping me @luishendrix92 @jenipha @RandellDawson @gabrieljim

OK now i’m much closer to understand what happened (before i see the jenipha post)
NOW i have fully understand what happened or what is behind the if single value

This what i understand from @luishendrix92 @jenipha posts :

We have a grope of truthiness and falseness value (String, Arrays…etc) and if use that if there is no condition inside it ? is that right ?

Thanks again for everyone
And special thanks to @luishendrix92 @jenipha :heartbeat::heartbeat::heartbeat:

Yeah there’s no condition because the parens of an if statement evaluate the expression inside of it and then determines the Boolean equivalent. To understand what evaluation means just go to the js console on your browser’s dev tools and type random stuff; whatever you see returned by the <- arrow is the result of an expression evaluation (a void function returns undefined).

Now, take that result and send it through a call to Boolean() to see what it returns.

That’s basically what happens in the if statement, all of the following are valid:

if (foo()) // ?? depends on the return value of foo
if (5 > 7) // true
if (true && '') // '' // falsy
if (true && 'hey') // 'hey' // truthy
if (true) // true
if (0) // falsy
if ("") // falsy
if ("hello") // truthy
if (6 > "5" && true) // true because "5" gets converted to number
if (new RegExp(/[aeiou]/)) // truthy
if ([]) // truthy
if ([1,2,3]) // truthy
if ({ let x = 5; x }) // truthy
if ({ foo: 'bar' }) // truthy

Among other crazy combinations you can think of. Notice how some combinations of boolean and other types result in odd results when put between boolean and comparison operators. This is a result of JS’s loosely typed nature. I also forgot to mention that undefined is also falsy.


The if condition is evaluated as a boolean.

if (true) {
   //this code is executed

if (2 < 3) {
   //this code is executed

if (ArielLeslieIsAwesome) {
   //this code is executed

if (false) {
   //this code is not executed

if (0) {
   //this code is not executed

if (undefined) {
   //this code is not executed
1 Like

Hey, WHAT IS THAT !!! it’s a very clear and greet explanation about how if work :blush: I think i haven’t any doubt now about what happen if single if value
Thank you again

Also thanks @ArielLeslie for your examples