-1

I am not so into JavaScript\TypeScript and I am finding the following problem with this code:

let timeStampAsDate = new Date();
let term = "2021";

console.log("XXX: ", timeStampAsDate?.getFullYear().toString().toLowerCase().includes(term) && timeStampAsDate != null);

let timeStampAsDate2 = undefined;

console.log("XXX: ", timeStampAsDate2?.getFullYear().toString().toLowerCase().includes(term) && timeStampAsDate2 != null);

Here you can run the code and see the output

I'll try to explain what is my problem:

As you can see in the previous code I have 2 different use cases, in the first one I first create a Date object and then I have a term representing an year that will be later used in a search (basically I am searching the inserted term representing a year into the year field of my date):

let timeStampAsDate = new Date();
let term = "2021";

console.log("XXX: ", timeStampAsDate?.getFullYear().toString().toLowerCase().includes(term) && timeStampAsDate != null);

If it is retrieved it print the Boolean value true. It works fine.

The second case is related to the use case where I have no date (this because in my application I can have undefined field of this type, because these data came from a Firebase database and can be not present):

let timeStampAsDate2 = undefined;

console.log("XXX: ", timeStampAsDate2?.getFullYear().toString().toLowerCase().includes(term) && timeStampAsDate2 != null);

Here I am not obtaining a Boolean value but in my console I am obtaining this output:

    XXX:  undefined

My idea is that having this && timeStampAsDate2 != null into my confition the final value have to be false but I discovered that in JavaScript\TypeScript an expression like this:

console.log(undefined && false)

give undefined as result and not false.

What can be a nice way to change my original condition in such a way that it is evaluated as false and not as undefined in the case that my timeStampAsDate2 is undefined?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • Why would you lowercase an int? – mplungjan Jun 14 '21 at 12:32
  • You can use the ["bang, bang you're boolean" operator](https://stackoverflow.com/questions/784929/what-is-the-not-not-operator-in-javascript#comment11728310_784929). – Ivar Jun 14 '21 at 12:32

2 Answers2

1

Why the string handling, just compare ints

We can add the !! to force a boolean on the undefined variable

let timeStampAsDate = new Date(2021,05,14,15,0,0,0); // today, to not make this weird next year
let term = 2021; // INT

console.log("XXX: ", !!timeStampAsDate && timeStampAsDate?.getFullYear() === term )

let timeStampAsDate2;

console.log("XXX: ", !!timeStampAsDate2 && timeStampAsDate?.getFullYear() === term )

// my code with the nullish coalescing operator is still much shorter

let timeStampAsDate3;

console.log("XXX: ",  timeStampAsDate3?.getFullYear() === term  ?? false)
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

The Nullish Coalescing Operator: (??) may be what you are after. It works really well with optional chaining. If the value is undefined it returns a default value.

let timeStampAsDate2 = undefined;
let term = "2021";
console.log("XXX: ", timeStampAsDate2?.getFullYear().toString().toLowerCase().includes(term) ?? false)
  • Still: Why string and tolowercase. – mplungjan Jun 14 '21 at 13:01
  • 1
    True @mplungjan! Love your recap! My main goal was to share about the Nullish Coalescing Operator for this specific issue. I wanted to leave the code as is as much as possible. Agreed, the toLowerCase is unnecessary. The toString may be helpful if the feature is to allow a user to type in a partial number (such as: 202 and get back 2020 or 2021). Unlikely but in any scenario, the solutions you summarized will be a good starting point! – Seth MacPherson Jun 14 '21 at 13:29
  • 1
    Also @mplungjan I cannot comment yet on your answer yet but one to add! If term is guaranteed (at least an empty string "") then `console.log("XXX: ", timeStampAsDate3?.getFullYear() === term)` should work too, as undefined will not equal a string. – Seth MacPherson Jun 14 '21 at 13:37