Skip to content

Instantly share code, notes, and snippets.

Created September 23, 2021 23:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
TypeScript JSON.parse undefined
const json = '{"foo": { "bar": { "baz": {"a": 1, "b": 2, "c": 3}}}}';
const parsedJSON = JSON.parse(json);
* DANGER! This line will run, but is not fail-safe, could crash if foo, bar,
* baz or a is missing or undefined
// This line will fail gracefully foo, bar, baz, or b is missing/undefined
// This line fails gracefully
// DANGER! DANGER! This line will crash
// console.log(parsedJSON?.foo?.bar?.baz?.x.y.z);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment