Is "if conditional" syntax different inside a for loop?

My code passes the challenge but this is the first time I’ve seen an “if conditional” with this syntax. Why isn’t the “if statement” wrapped in curly brackets and why isn’t it below the “if condition”? Please help me to understand, thanks.

  **Your code so far**

function bouncer(arr) {
let truthyArr = []
for(let i = 0; i < arr.length; i++){
  if(arr[i]) truthyArr.push(arr[i])
}
return truthyArr
}

bouncer([7, "ate", "", false, 9]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36

Challenge: Falsy Bouncer

Link to the challenge:

Two parts to this answer

  1. Whitespace does not have syntactic meaning. You could write the entire function on one line technically. We don’t because it makes code more difficult to read and reason about.

  2. If no braces follow an if (or else statement), then the next syntactic unit is taken to be the contents that would have gone into the braces. (Bonus knowledge! This is how else-if works!)

1 Like

You helped me to understand. Thanks @JeremyLT .

1 Like

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