TypeScript Nullish Coalescing Assignment Operator

TypeScript Tip: The ??= Operator Just learned about the Nullish Coalescing Assignment operator ( ??=) while building a logger utility! example would be this: long hand: if (singleton === null || singleton === undefined) { singleton = initLogger(); } You can write this: short hard: singleton ??= initLogger(); Perfect for: Singleton patterns ,Lazy initialization ,Default values , Caching strategies The beauty? It only assigns if the value is  null or undefined, not for other falsy values like 0 or "". #TypeScript #JavaScript #WebDev #CleanCode

To view or add a comment, sign in

Explore content categories