Is there a better / smarter way to do this?
theTitle = responsesToUse[i]["Title"];
if(theTitle == null)
theTitle = "";
Is there a better / smarter way to do this?
theTitle = responsesToUse[i]["Title"];
if(theTitle == null)
theTitle = "";
You can use the nullish coalescing operator. Returns the right side if the left side is null
or undefined
:
const theTitle = responsesToUse[i]["Title"] ?? "Default value";
Additionally you can use the logical or operator. It will check if left value is falsy
and return the right side:
const theTitle = responsesToUse[i]["Title"] || "Default value";
This question is answered here
Logical OR operator can be used:
const theTitle = responsesToUse?.[i]?.["Title"] || "";
use ?.
to check if the required index
/ property
exist in the source, else fallback to right side value.
Hope that helps.