Record Collection Solution Question

Hi all! NEw to coding, working my way through the Javascript course. A lot to take in on this one so odds are I’ll do it twice. But this is the first time I’ve had to look up a solution and saw something I didn’t recognize.

Here is the solution posted:

function updateRecords(id, prop, value) {
if(value === “”) delete collection[id][prop];
else if(prop === “tracks”) {
collection[id][prop] = collection[id][prop] || ;
} else {
collection[id][prop] = value;

return collection;

What I don’t understand specifically is

collection[id][prop] = collection[id][prop] || ;

I know that || is OR when it is in the conditional part of a statement, but in this case it is in the ACTION part of the if/then statement and I have no idea it’s purpose there. What does it do on that side?

So collection[id][prop] = collection[id][prop] || [ ]; just means assign collection[id][prop] to collection[id][prop], but if there is nothing inside collection[id][prop], it’s going to assign it to []. When you are assigning a variable, using || means a default, if the assignment is null or undefined, it will fallback to the default. So here’s an easier example:

let variable = undefined;
let variable2 = variable || "it is undefined";

console.log(variable2)//this will output "it is undefined", because I assigned
//an undefined variable to it, so it fallbacks to the default value.
1 Like

If the value on the left-hand side of the || evaluates to a truthy value, it will return that value. Otherwise, it will return the value on the right-hand side.

true || 'not getting to me'
// true

false || 'getting to me'
// "getting to me"

false || null
// null
const sayName = (name) => {
  return name || 'Please enter your name';

console.log(sayName()); // Please enter your name
console.log(sayName('John')); // John
1 Like

@lasjorg @Catalactics Thank you for the help! That is an awesome function! It’s come in handy a few lessons later.