How do i set a default value for a map (discord.js)

client.money = new Map();

instead of being “undefined”, how do i set a default value? (Like 0)
what i got for now is this but it is really unefficient

    if (client.money.get(ID) === undefined) {
      client.money.set(ID, 0);
      msg.reply("you have 0$")
    }

The code you’ve got is sensible and a good way of checking initial state to avoid junk data and bad state. It’s not inefficient really.

You don’t know all the user IDs that you’d want to populate up-front, so there’s no point saying “for each user ID, set it to zero by default”, and that would be a waste of time and resources anyway. The ‘Map’ type doesn’t provide a “default value” idea afaik.

You could re-write that block to be a little cleverer but I wouldn’t bother; it does what it means and it means what it says.

Keep going :grin:

2 Likes