Https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection

Tell us what’s happening:
Hi, im suprise with the highlighted row in part of code below from challenge Record Collection.
Please, can someone describe to me why the value in my last example is “new” and why is not “undefined” ?
Thank you very much.

Your code so far

//So, there is some examples, which describe, why i am cunfused. 

var x = 4 || "new";

console.log(x); // print 4

var y = "new" || 4;

console.log(y); // print new

var z = {

u: 3,

}

z.v = z.v;

console.log(z.v) // print undefined

var t = {

s: 3,

}

t.q = t.q || "new";

console.log(t.q) // print new!! but in each examples above print the value nearest to equal



// Only change code below this line
function updateRecords(id, prop, value) {

if (value === "") {
  delete collection[id][prop];
} else if (prop === "tracks") {
  **collection[id][prop] = collection[id][prop] || [      ];**
/*
  collection[id][prop].push(value);
} else {
  collection[id][prop] = value;
}
return collection;
}

// Alter values below to test your code
updateRecords(5439, "artist", "ABBA");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36.

Challenge: Record Collection

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/record-collection

So t has one property initially, t.s. There is no t.q. Setting t.q = t.q would evaluate as setting t.q to undefined. BUT… undefined is considered a “falsy” value (it can be coerced to a boolean false). So the OR (the ||) says “set t.q to the value of t.q, if it has a truthy value (one that doesn’t come back falsy) or to ‘New’.”

In the case of the record collection, that line says "If we have a “tracks” property on the collection[id], then leave that alone. If we don’t, then set it to an empty array. Doing that, the next line can simply push the value into an array, confident there is one, regardless if there was.

Thank you! I dont know undefined data is something like falsy data. I just learned in first challenge there is some data types like string, number or undefined.

there is a challenge later on about truthy and falsy values, it is called Falsy Bouncer

you don’t need this knowledge to solve this challenge, it is enough what you have lerned till now, like the hasOwnProperty method.