0

I am confused on why something works. Here's the issue :

<p>{params.value !==undefined || params.value!==null ? params.value.split("T")[0] : "no deadline set"}</p>

Triggers this error : Cannot read properties of null (reading 'split').

I translate the code as "if value is not undefined or not null display params.value.split("T")[0], else "no deadline set". But when I write this :

<p>{params.value && params.value !== undefined ? params.value.split("T")[0] : "no deadline set"}</p>

which I translate by if value AND value is not undefined display params.value.split("T")[0] else "no deadline set" It works.

I am confused on :

  1. Why we use && with same variable, makes no sense to me
  2. My understanding is that null and undefined are different, so when !== undefined, null should be params.value.split("T")[0], which will trigger the error (but it's not)
Vaodi
  • 37
  • 6
  • 1
    A pretty common question, exact duplicate of [Logical OR condition interpretation](https://stackoverflow.com/questions/75549884/logical-or-condition-interpretation). Use parenthesis to make your intention clear, don't guess at operator precedence – Andy Ray Mar 15 '23 at 03:08
  • 1
    Are you sure you want `||`, not `&&`? `params.value !==undefined || params.value!==null` is asking whether (it's defined or it's not null). Since `null !== undefined` that should always be true (modulo `params.value` being defined via a misbehaving getter). `params.value !== undefined && params.value !== null` would be true when `params.value` is neither `undefined` nor `null`. – Mike Samuel Mar 15 '23 at 03:09
  • `null` is not `undefined`, and `undefined` is not `null`, so your condition is *always* true due to using `||` (or). You need `&&` (and) to ensure *both* conditions are true. – Nick Mar 15 '23 at 03:11
  • Maybe just test that it is a string, therefore splittable: `typeof params.value === "string"`. – Mike Samuel Mar 15 '23 at 03:14

0 Answers0