-1

Please find below code I am using, and help me understand, where I am wrong:

let inf = ["love", "clean", "choose"];
let ger = ["like", "have", "smile"];



$('button').click(function(){
let word = document.getElementById("text1").value;

 if (word === inf[]) { 
  alert('this is working')
 };
abdel
  • 11
  • 2
  • 2
    https://www.w3schools.com/jsref/jsref_includes_array.asp – B. Go Dec 07 '19 at 20:52
  • 1
    Does this answer your question? [How do I check if an array includes a value in JavaScript?](https://stackoverflow.com/questions/237104/how-do-i-check-if-an-array-includes-a-value-in-javascript) – pr0gramist Dec 07 '19 at 20:54

2 Answers2

1

The correct way to check if an array contains something, is by using Array.prototype.includes(); For example:

let array = ['fruits', 'meat', 'idk'];
if (array.includes('fruits')) { // this if statement is true
   alert('this is working');
}

This way, you check for each element of the array if it is the one you put in the method.

0

let a = [1, 2, 3],
    b = [],
    c = 1
    
/*Check if a variable is an Array*/
console.log(a instanceof Array)
console.log(b instanceof Array)
console.log(c instanceof Array)

/*Check if array includes a certian value and return its index if avalable else returns -1*/
console.log(a.indexOf(2))
console.log(a.indexOf(3))
console.log(a.indexOf(5)) //[-1] Doesn't exist in the array
Ahmed Gaafer
  • 1,603
  • 9
  • 26