What does short circuit evaluation means

Hi
I am studying JavaScript

But i get stuck

records [id] [prop] = records [id] [prop] l l [
]

This part. They say it call short circuit evaluation.

If it check that. prop is tracks

Then they put our test values in right.

updateRecords(recordCollection, 5439, artists, ABBA)

So, they will go into records which is recordCollection . id which is 5439. prop which is artists.

Final answer is “artists”

What about the left side. it will get “artists” too ?

why do we need to compare it to [ ]

Here is the answer given

function updateRecords(records, id, prop, value) {
if (value === ‘’) {
delete records[id][prop];
} else if (prop === ‘tracks’) {
records[id][prop] = records[id][prop] || ; // this is called shortcircuit evaluation,
see below for explanation
records[id][prop].push(value);
} else {
records[id][prop] = value;
}
return records;
}

Thank you very much

  **Your code so far**

// Setup
var recordCollection = {
2548: {
  albumTitle: 'Slippery When Wet',
  artist: 'Bon Jovi',
  tracks: ['Let It Rock', 'You Give Love a Bad Name']
},
2468: {
  albumTitle: '1999',
  artist: 'Prince',
  tracks: ['1999', 'Little Red Corvette']
},
1245: {
  artist: 'Robert Palmer',
  tracks: []
},
5439: {
  albumTitle: 'ABBA Gold'
}
};

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

updateRecords(recordCollection, 5439, 'artist', 'ABBA');
  **Your browser information:**

User Agent is: Mozilla/5.0 (Linux; Android 11; V2025) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.82 Mobile Safari/537.36

Challenge: Record Collection

Link to the challenge:

records[id][prop] = records[id][prop] || [];

is functionally equivalent to

if (records[id][prop]) { // if the value already exists
    records[id][prop] = records[id][prop]; // keep it the same
} else { // if it doesn't
    records[id][prop] = []; // assign an empty array

The “short circuiting” term refers to the fact that the code stops evaluating as soon as it knows the result. In this case, if the left side of the || is truthy then the right side is never evaluated. If the left hand is falsy, then evaluation continues to the right side.

The term “short circuiting” is most often used in relation to boolean evaluations. In programming “or” (||) means “if either of this things is true, return true”.

let eitherTrue = (true || false); // the first one is truthy, so false doesn't matter (short circuit)
eitherTrue = (false || true); // the first one is false, so the second one needs to be checked

On the other hand, “and” (&&) means “both of these things are true”

let bothTrue = (false && true); // the first is false so the second doesn't need to be checked (short circuit)
bothTrue = (true && false); // the second value matters
1 Like

Thank you very very much

Hi

Your explanation helps me a lot

but for this quest I still have a question

They want that

  • Your function must always return the entire record collection object.
  • If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .
  • If prop is tracks but the album doesn’t have a tracks property, create an empty array and add value to it.
  • If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.
  • If value is an empty string, delete the given prop property from the album.

and in the solution 2

function updateRecords(records, id, prop, value) {
  if (value === '') {
    delete records[id][prop];

  } else if (prop === 'tracks') {
    records[id][prop] = records[id][prop] || []; // this is called shortcircuit evaluation, see below for explanation
    records[id][prop].push(value);
  
} else {
    records[id][prop] = value;
  }
  return records;
}

i checked it through it was like. These 2 hasn’t been included in the code or they did but I don’t understand how

  • If prop isn’t tracks and value isn’t an empty string, update or set that album’s prop to value .

  • If prop is tracks and value isn’t an empty string, add value to the end of the album’s existing tracks array.

Thank you very much

and can you explain how to use ===

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).