Checking through an array?

Hey all,

Ide like to use an if statement to check if an input field is a value is NOT contained in an array and can’t for the life of me figure out how to do it.

this is what im trying to do :point_down:

if (input.value.includes(!myArrayList[ ]) {…}

Assuming input is the input field and input.value is what the user inputs.

any ideas how to do this? I tried a few things but nothing works…

please help.

Wrong way round. It’s


This returns true or false, the whole function.


// a single value IS in some array
// a single value IS NOT in some array
someArray.includes(aSingleValue) === false
// or, same thing here:


arrayOfThings.includes(inputValue) === false
1 Like

So basically to elaborate on what im looking for,

if the user inputs “bob”, I want to first check myArrayList[ ] and see if bob is an array item. If NOT, then run the code block. if Bob is a part of the array (myArrayList), then don’t run the code block.

Hmmm so is there a way to not check for aSingleValue? but just a way to check through the array to see if a value is there?

Because I want the user to input anything. but if the input happens to not be included within the array, then run the code block.

Am I making sense or am I misunderstanding you?

hmm ok so aSingleValue could be be the user input? and this could check if the user input is a value that the array someArray holds?

@DanCouper Ive tried that method and I assumed it was wrong because my code block will always run. I know this because the variable I have inside the block will assign even if the conditions are not met

(searchMode = true) 

if(!keyWords.includes(inputBox.value) || !colors.includes(inputBox.value)) {
  q= inputBox.value;
  const dontGetIt =[
    "Sorry, Im having trouble understanding you.",
    "I didnt quite get that.",
    "I dont understand.",
    "Sorry, I dont know what that means yet.",

  mainParagraph.innerHTML = dontGetIt[Math.floor(Math.random()*dontGetIt.length)]+" "+ `Would you like me to search ${q}? Type "yes" or "no"`;

  searchMode = true;

So it seems when I have my if conditionals like this the code inside the brackets will run anyway…

Gimme a sec to figure out what you’re doing here, but just fyi so it’s a bit easier to read through stuff:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

How are you getting that value and what is that value when it unexpectedly gets past the condition (ie can you log it)?

And what are keywords and colors?

1 Like

Im assuming that you mean the ‘input box.value’ ? this is from a single input field.

This is basically anything I enter into the input that IS a part of the arrays.

so for example if I enter “bob”, which is a value in the array; the code block will run. and I don’t want it to. I want it to only run if what I enter into the input ISNT in the array.

those are the arrays that I am looking to check through, just like the someArray example you provided.

Yes, but if it’s always getting past that conditional regardless of what you type, then what that value is (and what the two arrays of values you’re checking against contain) becomes very important, because you are assuming one of these things is a value you expect, and it is not. If the value was in one of those arrays, the code in the block would not run, so if it is always running, then the value is not what you think it is.

1 Like

Im trying to understand. Im going to look at my code again…

yeah so

I have this code. and the real thing that is the problem is the searchMode variable assignment here.

so if I enter in a value that IS in one of the arrays here (being keyWords || colors)
the code block will run and searchMode is assigned TRUE. That causes a problem in my code later down the line. I need searchMode to remain FALSE, its original setting outside of the if statement here.

But as I noted, for some reason im console.logging search mode and im seeing that after that line in my code(the line inside the if statement in question), its returning true.

Put searchMode = false directly before the if condition. If I’m understanding you correctly, then you want to always run that condition, so turn off the search mode, run the condition. Can’t see affect on rest of code so this is just a suggestion but it may do what you want

1 Like

Ok ill give that a shot.

hmm no that didn’t work…

No, I want to only run the condition if what I enter into the input box is NOT a value within the arrays (keyWords or colors).

the problem is, is that the condition always seems to be running, thus making the serchMode value true.

Right, so if the condition always runs, then either the value is not what you think it is, or the value is not ever one of the values present in either of those two arrays, so need to know what they are and what value is

1 Like

I have a feeling the input value is a whole sentence, while the keywords and colors arrays are lists of words. @tuscannypolk is that right? If that’s the case you’d need to split the input value into words and then check if any of those words are included in the list.


haha im so confused by this. I think I get what you mean and it makes sense… but the thing that confuses me is that I don’t see how either of these could be the case. Even though the code is in fact behaving in such a way…

No, this is a good assumption but I started using single words as I was testing this issue just to see if everything was working correctly…

I think this has to be the case… but I don’t understand how…