[SOLVED] Using "item" in conditional statements

Looking at the code I found several times the use of a variable / object in as a condition such as


Game_Item.prototype.setObject = function(item) {
if (DataManager.isSkill(item)) {
this._dataClass = 'skill';
} else if (DataManager.isItem(item)) {
this._dataClass = 'item';
} else if (DataManager.isWeapon(item)) {
this._dataClass = 'weapon';
} else if (DataManager.isArmor(item)) {
this._dataClass = 'armor';
} else {
this._dataClass = '';
}
this._itemId = item ? item.id : 0;


The last line here or:


Game_Actor.prototype.changeEquip = function(slotId, item) {
if (this.tradeItemWithParty(item, this.equips()[slotId]) &&
(!item || this.equipSlots()[slotId] === item.etypeId)) {
this._equips[slotId].setObject(item);
this.refresh();
}
};


Third line. How exactly works this? The condition equals to true if the object is not null or is a special feature of Game_Item class? Also, what is the purpose in this last example using "!item"? Because I can't really get the condition...
this._itemId = item ? item.id : 0


means:


if (item == true)


  this._itemId = item.id


else


  this._itemId = 0


What do you want to know about !item or the third line?


! reverses the statement. !TRUE == FALSE, !FALSE == TRUE
this._itemId = item ? item.id : 0


means:


if (item == true)


  this._itemId = item.id


else


  this._itemId = 0


What do you want to know about !item or the third line?


! reverses the statement. !TRUE == FALSE, !FALSE == TRUE


But "item" is not a boolean and it's not equal to true, it should be a "Game_Item" instance. And about the "!item", I know that is the opposite/not of the variable but if the variable "item" is not a boolean, what exactly does it do?
if item == '0' -> FALSE


if item != '0' -> TRUE


item is set in the call


function(slotId, item)


but if item was set here to 0 or not set at all, than it is FALSE otherwise it is TRUE and ! just inverses the logical value. Just let's see what it means for the term item is used in


(!item || this.equipSlots()[slotId] === item.etypeId)


first


this.equipSlots()[slotId] === item.etypeId


if the strict value of the field in the array defined by slotId is equal to the value of item.etypeId -> TRUE


!item


if item has no value or the value '0' -> TRUE


||


The term is TRUE if either item is undefined / unset / '0' OR if the variables on the right side are even.


The term is only FALSE if item is defined AND the variables on the right side are uneven.


Edit: Usually boolean in most higher coding languages are integers, although you still have bit manipulation instructions.


Edit 2: There are other coding languages and cases when FALSE != '0' JFYI
Better now, thanks for your answer! 🙂  

Original thread: https://forums.rpgmakerweb.com/threads/solved-using-item-in-conditional-statements.57210/