Objective:
const jsonData = JSON.parse(this.description) ? JSON.parse(this.description) : null
When executing the above statement, my aim is to have the ability to parse a value successfully and return null if parsing fails. However, instead of achieving this desired behavior, I encounter an error message indicating Unexpected token at ....
. This error occurs because I am passing in data that cannot be parsed. My goal is to handle this scenario gracefully by returning a falsy value such as null rather than breaking the application entirely.
What I've Tried:
function checkParse(jsonString) {
try {
return JSON.parse(jsonString);
} catch (error) {
return null;
}
}
checkParse(description);
Unfortunately, the above code snippet results in an error stating Unexpected token l