DEV Community

Discussion on: Write More Robust JavaScript: 7 Best Practices

Collapse
 
supunkavinda profile image
Supun Kavinda • Edited
  1. Always consider using try/catch when using JSON.parse or JSON.stringify

I normally use a function with try/catch inside it so that I don't need to always add try/catch blocks.

export function decodeJSON() {
   // JSON.parse() with try/catch
}
export function encodeJSON() {
   // JSON.stringify() with try/catch
}