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
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])) {
newarr.push(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
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
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) {
newarr.push(arr[i])
}
}
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
@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))
@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.
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
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.
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
}
Hey, WHAT IS THAT !!! it’s a very clear and greet explanation about how if work I think i haven’t any doubt now about what happen if single if value
Thank you again