javascript – Check for Table null in Jquery for JSON response

Check the length before trying to index into the array.

var NE_LENGTH = (FSAInfoData.length == 0 ? 0 : FSAInfoData[0].UG_LENGTH) + FSAInfoData2[0].AR_LENGTH;

So if the length of the first table is 0, this defaults to 0 instead of FSAInfoData[0].UG_LENGTH.

Another method is to use optional chaining to prevent the error when trying to access the property of a nonexistent element, and ?? to supply the default value.

var NE_LENGTH = (FSAInfoData[0]?.UG_LENGTH ?? 0) + FSAInfoData2[0].AR_LENGTH;

Read more here: Source link