Different use of the equality operators

Hi

Having some issues here so hoping someone can shed some light on this for me.

In the part of code : if (prop !== ‘tracks’ && value !== “”),
If I use the (===) here instead of (==) I get a syntax error.

Its fine for the other parts of code but not when I negate the a boolean value (!==), it does not work.

In the piece of code above, I was under the impression that the properties of an object could be written as a string or other as it would be converted to a string and it seemed so in other tasks however, it returns a syntax error here without quotes.

What am I missing here?

I cant use dot notation in this code, anywhere I substitute it I get an error.

And why cant I use undefined instead of empty quotes? Why would there be a set of quotes with nothing for a value as an input where as undefined would work with no input?

Any help is much appreciated. I found this quite hard. I think I need to spend more time to thoroughly understand the material before I move onto another task.

So thanks ahead.

  **Your code so far**

// Setup
var collection = {
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(object, id, prop, value) {
if (prop !== 'tracks' && value !== "") {
  object[id][prop] = value;
} else if (prop === "tracks" && !object[id].hasOwnProperty("tracks")) {
  object[id][prop] = [value];
} else if (prop === "tracks" && value !== "") {
  object[id][prop].push(value);
} else if (value === "") {
  delete object[id][prop];
}
return object;
}

updateRecords(collection, 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/88.0.4324.182 Safari/537.36 Edg/88.0.705.74.

Challenge: Record Collection

Link to the challenge:

Just to clarify what you mean by trying ===, do you mean:

if (prop !=== 'tracks' && value !=== "") {

If so, there is no such comparison operator as !===. There is only != and !==

For added clarity, the ! in front of the == does not negate anything. The 3 symbols (!==) act as a single operator.

1 Like

Hi Randell
Thanks.

Okay so I have been thinking of the exclamation mark as the negate operator and not in a sequence with the equals operator. So thanks.

You can reference the value of an object’s property using bracket notation and the name of the property wrapped in quotes appearing between the two brackets, but if you do not wrap it in quotes, then JavaScript assumes it is a variable.

In the code where tracks is used in the conditional quotes are needed. But in the object collection tracks are not quoted. I didn’t think it mattered if quotes where used or not for an objects property?

I am not sure I am following what you are saying. Can you provide an actual code example of what you are saying?

Right, there is a difference. Here, where the object is being defined:

1245: {
  artist: 'Robert Palmer',
  tracks: []
},

tracks is defining the property. Technically, you could have put it is quotes, but there is no need. For these, quotes are only needed if the property is not a valid JS identifier (contains only alphanum, _, or $ and doesn’t start with a number).

Here:

} else if (prop === "tracks" && !object[id].hasOwnProperty("tracks")) {

They both must be in quotes because they are string literals. prop was passed in as a string so you are checking if it is the string "tracks", and then you are passing a string to the method hasOwnProperty. There is no JS data type of “property”, so we are passing in a string. Inside hasOwnProperty, it will do the proper JS to make the comparison.

This is different than using a property to access a value, where you can use dot notation or bracket notation (bracket notation is necessary if the property name is not a valid JS identifier or if the property name is stored in a variable.)

if (prop !== “tracks” && value !== “”)

Tracks here in quotes. I have another reply but will need to study it as I don’t know some of the terms and it take a bit to sink in. Thanks for your help.

Right, in that case “tracks” is in quotes because it is a string. The variable props is passed in as a string. There is no data type “property”.