Home
← All questions

Custom JSON.stringify in JavaScript

MediumPremiumJavaScript

Asked at ThoughtSpot · Multiplier

JavaScript interview practice: Write a JSON.stringify polyfill handling strings, numbers, booleans, null, arrays, objects, undefined/functions, and circular references.

Problem

Implement jsonStringify(value) matching JSON.stringify for the common cases:

  • primitives: string, number, boolean, null
  • arrays and plain objects (recursively)
  • undefined / functions → omitted in objects, become null in arrays
  • throw on circular references (like the native does)
function jsonStringify(value) {
  // your code
}
Premium question

Unlock the in-browser editor, test cases, and model solution with Mentoxis Premium.

Unlock with Premium