Set default value of boolean typescript











up vote
0
down vote

favorite












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?










share|improve this question






















  • try isMonday:boolean = false;
    – Fateme Fazli
    2 days ago












  • may be variabelName = false, is it?
    – Pardeep Jain
    2 days ago








  • 1




    @FatemeFazli this gives a lint error for redundancy (just to inform)
    – trichetriche
    2 days ago






  • 1




    @FatemeFazli also this is not recommended way
    – Pardeep Jain
    2 days ago






  • 1




    @trichetriche , PardeepJain thank you.
    – Fateme Fazli
    2 days ago















up vote
0
down vote

favorite












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?










share|improve this question






















  • try isMonday:boolean = false;
    – Fateme Fazli
    2 days ago












  • may be variabelName = false, is it?
    – Pardeep Jain
    2 days ago








  • 1




    @FatemeFazli this gives a lint error for redundancy (just to inform)
    – trichetriche
    2 days ago






  • 1




    @FatemeFazli also this is not recommended way
    – Pardeep Jain
    2 days ago






  • 1




    @trichetriche , PardeepJain thank you.
    – Fateme Fazli
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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?










share|improve this question













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?







angular typescript boolean






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 2 days ago









celamoet

1361211




1361211












  • try isMonday:boolean = false;
    – Fateme Fazli
    2 days ago












  • may be variabelName = false, is it?
    – Pardeep Jain
    2 days ago








  • 1




    @FatemeFazli this gives a lint error for redundancy (just to inform)
    – trichetriche
    2 days ago






  • 1




    @FatemeFazli also this is not recommended way
    – Pardeep Jain
    2 days ago






  • 1




    @trichetriche , PardeepJain thank you.
    – Fateme Fazli
    2 days ago


















  • try isMonday:boolean = false;
    – Fateme Fazli
    2 days ago












  • may be variabelName = false, is it?
    – Pardeep Jain
    2 days ago








  • 1




    @FatemeFazli this gives a lint error for redundancy (just to inform)
    – trichetriche
    2 days ago






  • 1




    @FatemeFazli also this is not recommended way
    – Pardeep Jain
    2 days ago






  • 1




    @trichetriche , PardeepJain thank you.
    – Fateme Fazli
    2 days ago
















try isMonday:boolean = false;
– Fateme Fazli
2 days ago






try isMonday:boolean = false;
– Fateme Fazli
2 days ago














may be variabelName = false, is it?
– Pardeep Jain
2 days ago






may be variabelName = false, is it?
– Pardeep Jain
2 days ago






1




1




@FatemeFazli this gives a lint error for redundancy (just to inform)
– trichetriche
2 days ago




@FatemeFazli this gives a lint error for redundancy (just to inform)
– trichetriche
2 days ago




1




1




@FatemeFazli also this is not recommended way
– Pardeep Jain
2 days ago




@FatemeFazli also this is not recommended way
– Pardeep Jain
2 days ago




1




1




@trichetriche , PardeepJain thank you.
– Fateme Fazli
2 days ago




@trichetriche , PardeepJain thank you.
– Fateme Fazli
2 days ago












4 Answers
4






active

oldest

votes

















up vote
3
down vote













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;
}





share|improve this answer




























    up vote
    1
    down vote













    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;
    }
    }





    share|improve this answer






























      up vote
      1
      down vote













      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.






      share|improve this answer























      • Downvoter, May I have an explanation?
        – Pardeep Jain
        2 days ago












      • stackoverflow.com/help/privileges/vote-down check here, clearly mentioned Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect. so my answer was neither incorrect nor unclear.
        – Pardeep Jain
        yesterday






      • 1




        Sounds great you know something new now, But dude offcourse its not about single upvote for me its about correct or incorrect. anyways thanks for removing downvote :)
        – Pardeep Jain
        yesterday


















      up vote
      0
      down vote













      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






      share|improve this answer























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














         

        draft saved


        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373572%2fset-default-value-of-boolean-typescript%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        3
        down vote













        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;
        }





        share|improve this answer

























          up vote
          3
          down vote













          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;
          }





          share|improve this answer























            up vote
            3
            down vote










            up vote
            3
            down vote









            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;
            }





            share|improve this answer












            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;
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 2 days ago









            trichetriche

            23.6k41949




            23.6k41949
























                up vote
                1
                down vote













                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;
                }
                }





                share|improve this answer



























                  up vote
                  1
                  down vote













                  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;
                  }
                  }





                  share|improve this answer

























                    up vote
                    1
                    down vote










                    up vote
                    1
                    down vote









                    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;
                    }
                    }





                    share|improve this answer














                    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;
                    }
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 2 days ago

























                    answered 2 days ago









                    Rohit.007

                    1,4082418




                    1,4082418






















                        up vote
                        1
                        down vote













                        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.






                        share|improve this answer























                        • Downvoter, May I have an explanation?
                          – Pardeep Jain
                          2 days ago












                        • stackoverflow.com/help/privileges/vote-down check here, clearly mentioned Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect. so my answer was neither incorrect nor unclear.
                          – Pardeep Jain
                          yesterday






                        • 1




                          Sounds great you know something new now, But dude offcourse its not about single upvote for me its about correct or incorrect. anyways thanks for removing downvote :)
                          – Pardeep Jain
                          yesterday















                        up vote
                        1
                        down vote













                        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.






                        share|improve this answer























                        • Downvoter, May I have an explanation?
                          – Pardeep Jain
                          2 days ago












                        • stackoverflow.com/help/privileges/vote-down check here, clearly mentioned Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect. so my answer was neither incorrect nor unclear.
                          – Pardeep Jain
                          yesterday






                        • 1




                          Sounds great you know something new now, But dude offcourse its not about single upvote for me its about correct or incorrect. anyways thanks for removing downvote :)
                          – Pardeep Jain
                          yesterday













                        up vote
                        1
                        down vote










                        up vote
                        1
                        down vote









                        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.






                        share|improve this answer














                        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.







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited yesterday

























                        answered 2 days ago









                        Pardeep Jain

                        35.6k1696133




                        35.6k1696133












                        • Downvoter, May I have an explanation?
                          – Pardeep Jain
                          2 days ago












                        • stackoverflow.com/help/privileges/vote-down check here, clearly mentioned Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect. so my answer was neither incorrect nor unclear.
                          – Pardeep Jain
                          yesterday






                        • 1




                          Sounds great you know something new now, But dude offcourse its not about single upvote for me its about correct or incorrect. anyways thanks for removing downvote :)
                          – Pardeep Jain
                          yesterday


















                        • Downvoter, May I have an explanation?
                          – Pardeep Jain
                          2 days ago












                        • stackoverflow.com/help/privileges/vote-down check here, clearly mentioned Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect. so my answer was neither incorrect nor unclear.
                          – Pardeep Jain
                          yesterday






                        • 1




                          Sounds great you know something new now, But dude offcourse its not about single upvote for me its about correct or incorrect. anyways thanks for removing downvote :)
                          – Pardeep Jain
                          yesterday
















                        Downvoter, May I have an explanation?
                        – Pardeep Jain
                        2 days ago






                        Downvoter, May I have an explanation?
                        – Pardeep Jain
                        2 days ago














                        stackoverflow.com/help/privileges/vote-down check here, clearly mentioned Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect. so my answer was neither incorrect nor unclear.
                        – Pardeep Jain
                        yesterday




                        stackoverflow.com/help/privileges/vote-down check here, clearly mentioned Use your downvotes whenever you encounter an egregiously sloppy, no-effort-expended post, or an answer that is clearly and perhaps dangerously incorrect. so my answer was neither incorrect nor unclear.
                        – Pardeep Jain
                        yesterday




                        1




                        1




                        Sounds great you know something new now, But dude offcourse its not about single upvote for me its about correct or incorrect. anyways thanks for removing downvote :)
                        – Pardeep Jain
                        yesterday




                        Sounds great you know something new now, But dude offcourse its not about single upvote for me its about correct or incorrect. anyways thanks for removing downvote :)
                        – Pardeep Jain
                        yesterday










                        up vote
                        0
                        down vote













                        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






                        share|improve this answer



























                          up vote
                          0
                          down vote













                          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






                          share|improve this answer

























                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            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






                            share|improve this answer














                            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







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited yesterday

























                            answered 2 days ago









                            CruelEngine

                            8641917




                            8641917






























                                 

                                draft saved


                                draft discarded



















































                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53373572%2fset-default-value-of-boolean-typescript%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                'app-layout' is not a known element: how to share Component with different Modules

                                android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

                                WPF add header to Image with URL pettitions [duplicate]