Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Right-hand side of instanceof is not callable

Tags:

javascript

So, i'm trying to make a simple Discord Bot using javascript, and I want to detect if a player username is on the banned list. I have an array

var banned = ['Andrew','David']

and an if

if (message.author.username instanceof banned) {.....

but when I run it, it outputs

if (message.author.username instanceof banned)
                            ^

TypeError: Right-hand side of 'instanceof' is not callable

What can I do?

like image 741
AlexejheroYTB Avatar asked Apr 19 '17 17:04

AlexejheroYTB


2 Answers

This is not what instanceof is for. instanceof is used to see if an object is an instance of a specific constructor (ex: banned instanceof Array).

If you just want to see if an element is in an array, you can use .indexOf().

if(banned.indexOf(message.author.username) != -1)
like image 128
Rocket Hazmat Avatar answered Sep 20 '22 10:09

Rocket Hazmat


instanceof is used to check if an object is an instance of a class. What you want to do is to check if a string is in an array like this:

if (banned.indexOf(message.author.username) >= 0) {...
like image 39
Rodrigo5244 Avatar answered Sep 22 '22 10:09

Rodrigo5244