Set default value of boolean typescript
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
add a comment |
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
tryisMonday:boolean = false;
– Fateme Fazli
Nov 19 '18 at 11:26
may bevariabelName = false
, is it?
– Pardeep Jain
Nov 19 '18 at 11:26
1
@FatemeFazli this gives a lint error for redundancy (just to inform)
– trichetriche
Nov 19 '18 at 11:27
1
@FatemeFazli also this is not recommended way
– Pardeep Jain
Nov 19 '18 at 11:31
1
@trichetriche , PardeepJain thank you.
– Fateme Fazli
Nov 19 '18 at 11:32
add a comment |
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
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
angular typescript boolean
asked Nov 19 '18 at 11:22
celamoetcelamoet
1671211
1671211
tryisMonday:boolean = false;
– Fateme Fazli
Nov 19 '18 at 11:26
may bevariabelName = false
, is it?
– Pardeep Jain
Nov 19 '18 at 11:26
1
@FatemeFazli this gives a lint error for redundancy (just to inform)
– trichetriche
Nov 19 '18 at 11:27
1
@FatemeFazli also this is not recommended way
– Pardeep Jain
Nov 19 '18 at 11:31
1
@trichetriche , PardeepJain thank you.
– Fateme Fazli
Nov 19 '18 at 11:32
add a comment |
tryisMonday:boolean = false;
– Fateme Fazli
Nov 19 '18 at 11:26
may bevariabelName = false
, is it?
– Pardeep Jain
Nov 19 '18 at 11:26
1
@FatemeFazli this gives a lint error for redundancy (just to inform)
– trichetriche
Nov 19 '18 at 11:27
1
@FatemeFazli also this is not recommended way
– Pardeep Jain
Nov 19 '18 at 11:31
1
@trichetriche , PardeepJain thank you.
– Fateme Fazli
Nov 19 '18 at 11:32
try
isMonday:boolean = false;
– Fateme Fazli
Nov 19 '18 at 11:26
try
isMonday:boolean = false;
– Fateme Fazli
Nov 19 '18 at 11:26
may be
variabelName = false
, is it?– Pardeep Jain
Nov 19 '18 at 11:26
may be
variabelName = false
, is it?– Pardeep Jain
Nov 19 '18 at 11:26
1
1
@FatemeFazli this gives a lint error for redundancy (just to inform)
– trichetriche
Nov 19 '18 at 11:27
@FatemeFazli this gives a lint error for redundancy (just to inform)
– trichetriche
Nov 19 '18 at 11:27
1
1
@FatemeFazli also this is not recommended way
– Pardeep Jain
Nov 19 '18 at 11:31
@FatemeFazli also this is not recommended way
– Pardeep Jain
Nov 19 '18 at 11:31
1
1
@trichetriche , PardeepJain thank you.
– Fateme Fazli
Nov 19 '18 at 11:32
@trichetriche , PardeepJain thank you.
– Fateme Fazli
Nov 19 '18 at 11:32
add a comment |
4 Answers
4
active
oldest
votes
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;
}
add a comment |
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.
Downvoter, May I have an explanation?
– Pardeep Jain
Nov 19 '18 at 11:30
stackoverflow.com/help/privileges/vote-down check here, clearly mentionedUse 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
Nov 20 '18 at 8:32
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
Nov 20 '18 at 8:47
add a comment |
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;
}
}
add a comment |
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
add a comment |
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',
autoActivateHeartbeat: false,
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Nov 19 '18 at 11:26
trichetrichetrichetriche
26.7k42255
26.7k42255
add a comment |
add a comment |
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.
Downvoter, May I have an explanation?
– Pardeep Jain
Nov 19 '18 at 11:30
stackoverflow.com/help/privileges/vote-down check here, clearly mentionedUse 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
Nov 20 '18 at 8:32
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
Nov 20 '18 at 8:47
add a comment |
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.
Downvoter, May I have an explanation?
– Pardeep Jain
Nov 19 '18 at 11:30
stackoverflow.com/help/privileges/vote-down check here, clearly mentionedUse 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
Nov 20 '18 at 8:32
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
Nov 20 '18 at 8:47
add a comment |
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.
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.
edited Nov 20 '18 at 8:31
answered Nov 19 '18 at 11:27
Pardeep JainPardeep Jain
39.5k16102139
39.5k16102139
Downvoter, May I have an explanation?
– Pardeep Jain
Nov 19 '18 at 11:30
stackoverflow.com/help/privileges/vote-down check here, clearly mentionedUse 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
Nov 20 '18 at 8:32
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
Nov 20 '18 at 8:47
add a comment |
Downvoter, May I have an explanation?
– Pardeep Jain
Nov 19 '18 at 11:30
stackoverflow.com/help/privileges/vote-down check here, clearly mentionedUse 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
Nov 20 '18 at 8:32
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
Nov 20 '18 at 8:47
Downvoter, May I have an explanation?
– Pardeep Jain
Nov 19 '18 at 11:30
Downvoter, May I have an explanation?
– Pardeep Jain
Nov 19 '18 at 11:30
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
Nov 20 '18 at 8:32
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
Nov 20 '18 at 8:32
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
Nov 20 '18 at 8:47
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
Nov 20 '18 at 8:47
add a comment |
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;
}
}
add a comment |
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;
}
}
add a comment |
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;
}
}
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;
}
}
edited Nov 19 '18 at 11:36
answered Nov 19 '18 at 11:30
Rohit.007Rohit.007
1,6312419
1,6312419
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 20 '18 at 5:22
answered Nov 19 '18 at 11:34
CruelEngineCruelEngine
1,0511919
1,0511919
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
try
isMonday:boolean = false;
– Fateme Fazli
Nov 19 '18 at 11:26
may be
variabelName = false
, is it?– Pardeep Jain
Nov 19 '18 at 11:26
1
@FatemeFazli this gives a lint error for redundancy (just to inform)
– trichetriche
Nov 19 '18 at 11:27
1
@FatemeFazli also this is not recommended way
– Pardeep Jain
Nov 19 '18 at 11:31
1
@trichetriche , PardeepJain thank you.
– Fateme Fazli
Nov 19 '18 at 11:32