Iam trying to add Conditions in a variable and then assign in if() condition, but its not working as expected.
Tried Possibilities :
1)
conditionCheck = (getMonth == undefined || getMonth == "" || getMonth == null
|| getDay == undefined || getDay == "" || getDay == null
|| getYear == undefined || getYear == "" || getYear == null )
2)
conditionCheck = getMonth == undefined || getMonth == "" || getMonth == null
|| getDay == undefined || getDay == "" || getDay == null
|| getYear == undefined || getYear == "" || getYear == null
3)
conditionCheck = "getMonth == undefined || getMonth == "" || getMonth == null
|| getDay == undefined || getDay == "" || getDay == null
|| getYear == undefined || getYear == "" || getYear == null"
But if i add as is in the condition then its working fine.
like this. - >
if ( getMonth == undefined || getMonth == "" || getMonth == null
|| getDay == undefined || getDay == "" || getDay == null
|| getYear == undefined || getYear == "" || getYear == null ) {
} else {
ageCalculation();
}
Any Idea / suggestion ?
Edited :
function ageCalculation() {
getDate = getYear + "-" + getMonth + "-" + getDay;
dob = new Date(getDate);
today = new Date();
age = (today - dob) / (365.25 * 24 * 60 * 60 * 1000);
if (age < 3 || age == 3 || age > 3 && age < 3.00452422294471007) {
$('.greater-msg, .less-then-msg').remove();
$(contentParent).find('.fieldset-wrapper').after('<div class="less-then-msg">Disclaimer: In compliance with EO51, cannot directly engage with mothers with children aged 0 to 3 years old. All content that you will receive via email will only be regarding your pregnancy. </div>');
} else if (age > 3) {
$('.greater-msg, .less-then-msg').remove();
$(contentParent).find('.fieldset-wrapper').after('<div class="greater-msg">You can also visit to know how you can keep giving your child the 360 advantage.</div>');
}
if (age <= -1 || age <= -0 || age == 0 || age == -0) {
$('.greater-msg, .less-then-msg').remove();
}
}
Thanks!!
TL;DR; In JS, to check whether variable
is neither null
, undefined
or empty
is simply just by,
if (variable) {
// 'variable' is not null, undefined or ''.
}
Your first and second condition should work just fine.
Be aware, Date.getMonth() returns 0-based index of the month. And if it just happens that month is
January
, your condition will no longer work sincegetMonth
is0
and will turn yourconditionCheck
to be alwaystrue
.Because
0 == ""
.
As for your third condition, which is a string, will always return true
when wrapped in if
condition.
An alternative way,
So, you want to run ageCalculation
function if getMonth
, getDay
, and getYear
are not undefined
, null
or empty
.
So, instead of (Your current code)
if (getMonth == undefined || getMonth == "" || getMonth == null || getDay == undefined || getDay == "" || getDay == null || getYear == undefined || getYear == "" || getYear == null) {
// ...
} else {
ageCalculation();
}
As I state above, For checking whether variable is either undefined
or null
or even ""
(empty), you can modify your code to,
if (getMonth && getDay && getYear) {
// getMonth, getDay, and getYear are not null, undefined or "".
ageCalculation();
}
else {
// Either getMonth, getDay and getYear have a value of null, undefined or "".
}
You can use JS Ternary Operator. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
var getMonth;
var getDay;
var getYear;
var conditionCheck = (getMonth == undefined || getMonth == "" || getMonth == null || getDay == undefined || getDay == "" || getDay == null || getYear == undefined || getYear == "" || getYear == null) ? "Hello World" : "Hello Universe!"
console.log(conditionCheck) // Hello World
if( myVariable ) {
//If myVariable's value is not: null OR empty string (“”) OR undefined OR NaN OR false OR 0
} else {
//If myVariable's value is equal to null OR empty string (“”) OR undefined OR NaN OR false OR 0
}
So, Ternary Operator can be as :
var getMonth;
var getDay;
var getYear;
var conditionCheck = (getMonth || getDay || getYear) ? '1':'0';
console.log(conditionCheck); // displays 1 if either getMonth, getDay, getYear have values other than null OR empty string (“”) OR undefined OR NaN OR false OR 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With