0

I have a user database like this:

const user = {
  subscription: {
    plan: 'free_trial',
  },
};

I need to check some condition before user changes plan.

const currentDate = new Date();
if (user.subscription.trialExpDate > currentDate) {
  // do something
} else {
  // trialExpDate is either undefined or <= currentDate
  user.subscription.trialExpDate = currentDate;
}

My question is, for some users trialExpDate will be undefined. Is it okay to compare undefined against currentDate object? Or do I need to check if trialExpDate exist first?

6
  • 1
    What is user.subscription.trialExpDate ? Is it a string ? Try new Date(user.subscription.trialExpDate) Commented Oct 2, 2016 at 3:33
  • user.subscription.trialExpDate is either undefined OR a date object Commented Oct 2, 2016 at 3:34
  • 1
    undefined > new Date(); will always be false.. I would have tested the value before comparing.. Commented Oct 2, 2016 at 3:36
  • yeah that was my question. so you would test if it's undefined first before doing the rest of the code? even if it returns same answer and shorter code? Commented Oct 2, 2016 at 3:38
  • 1
    I certainly would have tested it.. That would make my code more readable.. Commented Oct 2, 2016 at 3:39

3 Answers 3

3

I would suggest check with hasownproperty. Sample:

if (user.subscription.hasOwnProperty('trialExpDate') && user.subscription.trialExpDate > currentDate) {
  // do something
} else {
  // trialExpDate is either undefined or <= currentDate
  user.subscription.trialExpDate = currentDate;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks I'll do that instead :)
trialExpDate is not ownProperty of user object, it belongs to user.subscription
What if user.subscription.trialExpDate is null ? It will return hasOwnProperty true.. I will go with if(user.subscription.trialExpDate && .....)
Yes, @Rayon that is one scenario. If we are confident that won't be null we can use hasownproperty otherwise your we have to check as you said
1

You can just check if it is null.

if (user.subscription.trialExpDate != null || user.subscription.trialExpDate > currentDate) {
    // do something
}
else {
    // do something else
}

The variable != null will simultaneously check if the variable is null or undefined.

3 Comments

What if user.subscription.trialExpDate is 0 ? I would have had a function to test if it is a valid Date..
Then he could check as well if it is indeed 0.
0

In short: if you're sure that user.subscription.trialExpDate cannot be a null, using your original code is very okay.

See how the JavaScript relational comparison operators coerce types.

If user.subscription always exist and it is always an object, comparison between a Date object, and an undefined or a NaN, is evaluated as false. However, for a null, it is evaluated as +0, thus null < (new Date) will be true, null > (new Date) will be false.

When JavaScript relational comparison works,

  1. A Date object is converted to its timestamp, which is (The Date object).valueOf().

  2. A primitive is converted to a number, which means:

    • an undefined is converted to a NaN;
    • a null is converted to +0.
  3. Then the comparison is performed between each item as you'd expect for the operator. Note that any comparison involving a NaN evaluates to false.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.