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?
angular typescript boolean
add a comment |
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?
angular typescript boolean
tryisMonday:boolean = false;
– Fateme Fazli
2 days ago
may bevariabelName = 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
add a comment |
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?
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 2 days ago
celamoet
1361211
1361211
tryisMonday:boolean = false;
– Fateme Fazli
2 days ago
may bevariabelName = 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
add a comment |
tryisMonday:boolean = false;
– Fateme Fazli
2 days ago
may bevariabelName = 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
add a comment |
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;
}
add a comment |
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;
}
}
add a comment |
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.
Downvoter, May I have an explanation?
– Pardeep Jain
2 days ago
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
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
add a comment |
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
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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 2 days ago
trichetriche
23.6k41949
23.6k41949
add a comment |
add a comment |
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;
}
}
add a comment |
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;
}
}
add a comment |
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;
}
}
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 2 days ago
answered 2 days ago
Rohit.007
1,4082418
1,4082418
add a comment |
add a comment |
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.
Downvoter, May I have an explanation?
– Pardeep Jain
2 days ago
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
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
add a comment |
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.
Downvoter, May I have an explanation?
– Pardeep Jain
2 days ago
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
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
add a comment |
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.
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 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 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
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
add a comment |
Downvoter, May I have an explanation?
– Pardeep Jain
2 days ago
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
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
add a comment |
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
add a comment |
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
add a comment |
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
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 yesterday
answered 2 days ago
CruelEngine
8641917
8641917
add a comment |
add a comment |
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
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