JavaGamer
JavaGamer

Reputation: 51

How can I check if an array is null

I want to make a command, so that i attach roles in discord. when i want to attach allowed roles, it work, when i want to atach unallowed roles (like admin, team, etc) it works too and denies. but when I just type !role, it outputs nothing. That isn´t bad, sure, but after some tries, i realized that the parameters, when i don´t enter a single one, aren´t null. I tried it with

if(args == null) {
            message.channel.send(`You haven´t entered a role!`);
    }

args is the array, where the command parameters are saved. I know that without one single parameter, the array doesn´t even exist, so how can I tell the bot "If there is no array coming with the command, then..."

Upvotes: 1

Views: 69

Answers (1)

KFE
KFE

Reputation: 638

The code below will evaluate to true if the args variable is not an array or if the array does not have at least one value.

if (!Array.isArray(args) || args.length === 0) { message.channel.send(`You haven´t entered a role!`); }

Upvotes: 4

Related Questions