Check if object is deep frozen

JavaScript, Object, Recursion · Oct 20, 2020

Checks if an object is deeply frozen.

const isDeepFrozen = obj =>
  Object.isFrozen(obj) &&
  Object.keys(obj).every(
    prop => typeof obj[prop] !== 'object' || isDeepFrozen(obj[prop])
  );
const x = Object.freeze({ a: 1 });
const y = Object.freeze({ b: { c: 2 } });
isDeepFrozen(x); // true
isDeepFrozen(y); // false

More like this

  • Compact object

    JavaScript, Object

    Deeply removes all falsy values from an object or array.

  • Check object equality

    JavaScript, Object

    Performs a deep comparison between two values to determine if they are equivalent.

  • Deep merge objects

    JavaScript, Object

    Deeply merges two objects, using a function to handle keys present in both.