Trying to understand this use of ||

This is from “record collection”. FCC isn’t letting me ask from the “Get Help” button for some reason.

This code is from the 3rd solution. Can anyone explain this line? It seems to say if album[“tracks”] exists, continue on the next line and if album[“tracks”] doesn’t exist, create an empty array and continue on the next line.
But from what I have understood about || so far is that if either side is true it will run the next line of code. But in this case it seems to be actually doing something rather than just giving you a true or false answer, which is throwing me off. The full solution is pasted below. Thanks

album["tracks"] = album["tracks"] || [];

Full Solution:

function updateRecords(records, id, prop, value) {
  // Access target album in record collection
  const album = records[id];
  if (value === "") {
    delete album[prop];
  } else if (prop !== "tracks") {
    album[prop] = value;
  } else {
    album["tracks"] = album["tracks"] || [];
    album["tracks"].push(value);
  }
  return records;
}

It’s one of the common uses of the Logical OR operator.

Short-circuit evaluation is what is going on here.

Essentially it defaults to whichever value is truthy.

Still not getting it. In one of the test cases, album[“tracks”] doesn’t exist, so neither album[“tracks”] nor empty array can be true. So how is this working in that case?

An empty array is a truthy value.

The short-circuit OR evaluation doesn’t really care if the value on the right is true or false, or whatever. It only tests the value on the left. If that is “truthy” then it returns it and it never even touches the value on the right (that’s the short-circuit). But if the value on the left is “falsey” then it returns the value on the right, regardless of what its value is.

So is it basically saying: if album[“tracks”] doesn’t exist (isn’t true), create it and give it a value of empty array? One of the tests doesn’t have an album[“tracks”] property, and this line seems to create one and give it the value of an empty array, somehow.

Pretty close. It’s really saying: If album["tracks"] doesn’t have a “truthy” value then set it to an empty array.

The “truthy” is important here. If album["tracks"] has a value of 0 then it will be “falsey” and it will get set to an empty array. Not that it would have a value of 0 in this particular challenge. It’s just something you need to be aware of in general.

One of the test cases doesn’t have an album[“tracks”] property to set to an array. Is this statement creating that property and then giving that property that it created the value of an empty array? Or am I missing something?

What value is returned if you try to access album["tracks"] but the album object does not have a tracks property? Is that value truthy or falsey?

It returns “undefined”. That would be false right?

Correct, so in that case the expression won’t short circuit and album["tracks"] will be set to an empty array.

1 Like

You mean album[“tracks”] will be created, and then have it’s value set to an empty array? Or is the property being created somewhere else?

Ok, so both are being done by this statement right?
album["tracks"] = album["tracks"] || [];

Ok, great. Thanks for your time!

Thanks for your time!

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