Problem in JavaScript Algorithms and Data Structures om "Record Collection"

As per the solution, the line

if(collection[id][prop]) {

is not Boolean. Can anybody explain how is it Boolean?

There are properties that each item in the collection may or may not have, like artist’s name. you can check the existence of each property with an if statement. If there’s not an equality operator, you can read a statement like if (x) as “if x exists, then do this…” or as “if there is an x, then do this…”. These ways are how I read code like that, although I guess it more accurately or verbose reading would be as “if x exists, then true, and if true then do this…”

(Here’s the link for the lesson for anyone else.)

1 Like

It is not Boolean but everything can be considered a truthy or falsy value
undefined is falsy, which is what you get if that property is not present
A string or an array is truthy (but an empty string is falsy!)
An if statement coerce truthy and falsy values to a Boolean, executing for truthy values and not for falsy values
At the end if x exist like @BurnChao was saying is a wonderful interpretation

If you want to experiment if something is considered truthy or falsy use console.log() adding in front of the value !!
So for example console.log(!!undefined) will print false, so undefined is falsy

2 Likes

As @ilenia explained, all values can be treated as logically binary. If you go back to earlier in the JavaScript curriculum, that is the lesson taught by Falsy Bouncer.

1 Like

Thanks @BurnChao for clarifying.

Thanks @ilenia for sharing & clarifying.