[SOLVED] Check if something is true

It is still a bit unclear to me how I can and cannot check whether something is true.

Suppose I make a boolean called isTrue.

I know that this is working:

if (!isTrue) {
    //  this is not true
}

But is this also working:

if (isTrue) {
    //  this is true
}

Or is this always true bacause this is checking if the boolean exist?

As:

if (isTrue) {
    //  isTrue is not null
}

You don’t make a boolean called true. true is a JavaScript keyword (the literal value for a boolean type).

So, under normal circumstances:

if (!true) {
    // Code in here will NEVER execute
}

Oke oke, bad choice. :sweat_smile:

Are you asking what the ! operator means? Not 100% sure what is being asked here?

No, I’m not sure if I can use this:

if (isTrue) {
    //  this is true
}

Or do I have to use this:

if (isTrue === true) {
    //  this is true
}

I clarified my question above with an additional example.

You can usually use both.

The difference between the two statements is subtle, and it has to do with assigning or checking against undefined at the same time.

If your variable holds only true or false as a value, then you are perfectly fine using the short form.

Oke great. Good to know.

I got this question because a question from another user made me doubt how the statement will be read. (I also don’t know exactly how the user intended it, but I assume that the user means the current material of the mesh).

KeyboardHandler.attributes.add('redMaterial', {
    type: 'asset',
    assetType: 'material'
});

KeyboardHandler.prototype.onKeyDown = function (event) {
    if (event.key === pc.KEY_A && this.redMaterial) {
        this.entity.model.meshInstances[0].material = this.redMaterial.resource;
    }
};

When in the statement above is this.redMaterial true and when is it false?

If no asset has been assigned in the editor, then this.redMaterial is undefined and evaluate as false in the if statement.