Singleton pattern in JavaScript

Singleton pattern can be achieved quite trivially in JavaScript with the help of Immediately Invoked Function Expression, also known as IIFE

The idea is to directly call a function as it is declared.

Let’s say we have a class conveniently called Singleton that we want to instantiate just once over the course of our application. One way of going about it is as follows -

const getInstance = (() => {
  let instance = null;
  return () => {
    if (instance == null) {
      instance = new Singleton();
    }

    return instance;
  };
})();

const i = getInstance();

References