mardi 18 août 2020

Angular: Why in an 'if statement' you can use a const as a boolean?

I've found a lot of examples in Angular using an if statement the following way.

Here the if statement is checking if the "id" const exists or not. If it exists it executes the service task. However, "id" is not a boolean, is a number, so I don't understand why it is analyzed as a boolean.

alumno: Alumno = new Alumno();

constructor(private service: AlumnoService, private route: ActivatedRoute){}

this.route.paramMap.subscribe(params => {
      const id: number = +params.get('id');
      if(id){
        this.service.ver(id).subscribe(alumno => this.alumno = alumno);
      }
    })

I suspect that if "id" is not null, then it is analyzed as true, and if it is null is analyzed as false, but I don't understand why.

Aucun commentaire:

Enregistrer un commentaire