Confused with the use of the if statement and it's conditions

Tell us what’s happening:
I don’t completely understand how the if statement in this code solution works. I’m going through the JavaScript courses again as I don’t feel I have a comfortable grasp on it yet.

Question 1: Aren’t If statements supposed to look something like this instead? →

if ((arr[i]) result.push(arr[i])) {
}

Question 2: Also, what exactly is this if statement doing? How is arr[i] a condition? Isn’t arr[i] just a copy of the iteration? I know that if statements stop once they reach a falsy value, but what if there is more than one falsy value? Would the for loop just stop at the first falsy value?

I hope this question makes some sense. Please let me know what I can do to clarify.

I do not understand the If statement

function func(array) {

var resultArray = []; 

for (let i = 0; i < array.length; i++) {
// ================== If statement that I don't understand below
  if (arr[i]) result.push(arr[i]);    // line that I don't understand
// are singular if statements without curly brackets normal? 
     }
   return result;
}

func([7, "string", " ",  false, 3])
// [7, "string", " ", 3]

Challenge: Falsy Bouncer

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

this can be expanded as

if (arr[i]) {
   result.push(arr[i]);
}

If you don’t use curly brackets in an if statement, only the next expression will be inside the if statement (it works the same for loops)

1 Like

an if statement execute if its condition is true, and doesn’t if the condition is false. If the condition is not a boolean it is evaluated to a boolean.
Values in Javascript can be truthy or falsy, they are falsy if when converted to a boolean they evaluate to false, and truthy if evaluate to true.
Falsy values are 0, null, "" (empty string), [] (empty array), undefined and maybe one or two others…
Everything else is truthy.

the loop execute as long as its condition is true,

or if a return or break is executed inside it (in this case they are not there so above applies)

Thank you, I was thinking that if statements only executed if the conditions were explicitly true/false. I didn’t think to take into account truthy/falsy values.

Understood, I was thinking if statement expressions had to be inside of curly brackets in order to work!

contemporary JS compilers allow for code shorthands, like omitting semicolon in the end of a line, or not using curly brackets to wrap an action linked to a condition, if that action code can fit on the same line. When you want to queue more complex tasks when a condition is true, then you gotta wrap your code in curly brackets.
Regarding the “intuitive” condition statement use, its common practice to simply link to some variable value, and if say, it doesnt exist, returns undefined, or is zero, the condition would be rendered as false, otherwise it would match the statement.
For example you can make a condition to run only if an array owns any elements by using if (array.length). This way, whenever its length is zero, the statement will be false(instead of being explicit and writing array.length===0). Or in situations when you expect a user to input some string and only execute an action, when the user actually inputs anything, you can simply put if (userInput), which would return fasle when it gets an empy string (''). There are very clever ways to write your conditions.
PS: if you put ! before your statement, it will be true only if the returned value is false, e.g. if (!array.length) will be true only when the array has no elements, or you can say if (!someString), which will match when the string is empty.

Wow! This definitely helps out. I usually ask myself questions in terms of what’s going on “under the hood” when it comes to new syntax or methods that aren’t understandable at first glance so this helps a lot. Thanks!

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