ES6 onwards we have const.
This is not allowed:
const x; //declare first
//and then initialize it
if(condition) x = 5;
else x = 10;
I suggest this solution with a Singleton Pattern implementation:
var Singleton = (function () {
var instance;
function createInstance() {
// all your logic here
// so based on your example:
// if(condition) return 5;
// else return 10;
}
return {
getInstance: function () {
if (!instance) {
instance = createInstance();
}
return instance;
}
};
})();
const x = Singleton.getInstance();