-1

I wanted to achieve a value in the array is present in the array variable or not.

var a = [1, 2, 3];
if (a === 1) {
    alert("green");
}

So my goal is to check in the variable a holds the value 1 or not.

0

2 Answers 2

3

Use includes:

let a = [1, 2, 3]

if (a.includes(1))
  console.log('exist');
else
  console.log('not exist');

Sign up to request clarification or add additional context in comments.

3 Comments

This is what i want. Thanks Omri
@SagarSinha although it's a duplicated question, if that's the right answer you should mark it.Good Luck!
I have upvoted and accepted
0

You need to loop through array members and check if any of the members has that value. Here's an example:

var a = [1,2,3];
for(let i = 0; i < a.length; i++){
   if(a[i] == 1){
      alert("green");
   }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.