Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2: what differences between comparison operators == and === in ngIf directive

I don't understand why these two operators exist. In case of boolean comparison both == and === seem to work, but in case of enum comparison only '==' works:

<div class="interventionGroup">

    <div class="interventionGroupHeader transition_1s" (click)="onClickHeader()">
        {{GroupName}}
        <div *ngIf="expanded == true" class="expand-icon"><i class="material-icons">expand_less</i></div>  <!-- WORKS -->
        <div *ngIf="expanded === false" class="expand-icon"><i class="material-icons expand-icon">expand_more</i></div> <!-- WORKS -->
    </div>

    <button *ngIf="GroupType == GroupTypeEnum.mesInterventions">dfdsfsd</button> <!-- WORKS -->

    <div style="list-style-type:none" *ngIf="expanded === true">
        <div *ngFor="let intervention of interventions"
            (click)="onClick(intervention)"> 
            <intervention-button [intervention]="intervention"></intervention-button>
        </div>
    </div>
</div>
like image 305
Anthony Brenelière Avatar asked Aug 26 '16 09:08

Anthony Brenelière


People also ask

What is the difference between == and === in Angular?

The difference between == and === is that: == converts the variable values to the same type before performing comparison. This is called type coercion. === does not do any type conversion (coercion) and returns true only if both values and types are identical for the two variables being compared.

What is === operator in Angular?

=== is used for comparision between two variables but this will check strict type, which means it will check datatype and compare two values.

What does === comparison operator do?

The strict equality operator ( === ) checks whether its two operands are equal, returning a Boolean result. Unlike the equality operator, the strict equality operator always considers operands of different types to be different.

What is the difference between == and === in Javascript?

The main difference between the == and === operator in javascript is that the == operator does the type conversion of the operands before comparison, whereas the === operator compares the values as well as the data types of the operands.


1 Answers

In javascript, the operator '==' only check equality and '===' check type and equality

0 == '0' => true
0 === '0' => false
like image 184
wilovent Avatar answered Oct 08 '22 22:10

wilovent