13

I have an object:

export class ReccurrenceModel {
    fromDate: Date;
    toDate: Date;
    weeklyReccurrence: number;
    state: State;
    isMonday: boolean;
    isTuesday: boolean;
    isWednesday: boolean;
    isThursday: boolean;
    isFriday: boolean;
    fromDateToReturn: Date;
    toDateToReturn: Date;
}

I use it like this

  if (this.reccurrenceSelected === true) {
      this.reccurrence.isMonday = this.mondaySelected;
      this.reccurrence.isTuesday = this.tuesdaySelected;
      this.reccurrence.isWednesday = this.wednesdaySelected;
      this.reccurrence.isThursday = this.thursdaySelected;
      this.reccurrence.isFriday = this.fridaySelected;
}

I want to set a default value for them - false because if I do not set them in in UI, they will be undefined and I don't want that.

How to set de default value of a boolean in typescript?

user2004
  • 1,783
  • 4
  • 15
  • 42

5 Answers5

14

Doesn't make any big change in UI level you can use either. Both are falsy values for UI.

You can set anyone.

variableName = false 

or

variableName: boolean;

variableName you can use either UI consider it as false by default untill you assign its value to true.

Pardeep Jain
  • 84,110
  • 37
  • 165
  • 215
6

I would add a default constructor and do something like this :

export class ReccurrenceModel {
    fromDate: Date;
    toDate: Date;
    weeklyReccurrence: number;
    state: State;
    isMonday: boolean;
    isTuesday: boolean;
    isWednesday: boolean;
    isThursday: boolean;
    isFriday: boolean;
    fromDateToReturn: Date;
    toDateToReturn: Date;

    constructor(){
      this.isMonday = false;
      this.isTuesday = false;
      this.isWednesday = false;
      this.isThursday = false;
      this.isFriday = false;
    }
}

later i would do something like this : ,

this.reccurrence = new ReccurrenceModel();

The above line would initialize the required fields. you can confirm this by doing a console.log(this.reccurrence) after calling the constructor

CruelEngine
  • 2,701
  • 4
  • 23
  • 44
5

undefined, as well as false, are both falsy values that you can test the same way.

But default values are set with

export class ReccurrenceModel {
    fromDate: Date;
    toDate: Date;
    weeklyReccurrence: number;
    state: State;
    isMonday = false;
    isTuesday = false;
    ...
    fromDateToReturn: Date;
    toDateToReturn: Date;
}
1

I would suggest to use getters and setters in class

export class ReccurrenceModel {
    fromDate: Date;
    toDate: Date;
    weeklyReccurrence: number;
    state: State;
    isMonday: boolean;
    isTuesday: boolean;
    isWednesday: boolean;
    isThursday: boolean;
    isFriday: boolean;
    fromDateToReturn: Date;
    toDateToReturn: Date;
        ...

        get fromDate() {
            return this.fromDate|| "";
        }
        get isMonday() {
            return this.isMonday|| false;
        }
    }
Rohit.007
  • 3,414
  • 2
  • 21
  • 33
1

You can put

      this.isMonday = false;
      this.isTuesday = false;
      this.isWednesday = false;
      this.isThursday = false;
      this.isFriday = false;

these in ngOnInit() method also, this is the method which is called first. to find the difference between ngOnInit() and constructor visit Difference between Constructor and ngOnInit