-1

I'm using Javascript to find if something is in an array or not, but when I'm using this code it doesn't alert anything at all, which means it's not working correctly. What am I doing wrong?

var theArray = new Array("one","two","three");

if (theArray.indexOf("one")) { alert("Found it!"); }
if (!theArray.indexOf("four")) { alert("Did not find it!"); }
frosty
  • 2,559
  • 8
  • 37
  • 73

6 Answers6

2

you should use the includes function as:

if (theArray.includes('one')) { alert("Found it!"); }
Observablerxjs
  • 692
  • 6
  • 22
2

Remember that the starting index of an array is 0.

From the docs.

The indexOf() method returns the first index at which a given element can be found in the array, or -1 if it is not present.

The index of one is 0, which is falsy so the first check will fail. indexOf will return -1 if there is no match so you should explicitly check for that.

var theArray = new Array("one","two","three");

if (theArray.indexOf("one") !== -1) { alert("Found it!"); }
if (theArray.indexOf("four") === -1) { alert("Did not find it!"); }
Jim Wright
  • 5,905
  • 1
  • 15
  • 34
1

That's because indexOf returns a number which is the index of the matched string, and -1 if the string does not exist in the array. You need to compare the return value to numbers like these:

if (thisArray.indexOf('one') > -1) { alert('Found it!') }
if (thisArray.indexOf('four') > -1) { alert('Did not find it') }

You can use includes to return a boolean instead, if you'd like.

if (thisArray.includes('one')) { alert('Found it!') }
djfdev
  • 5,747
  • 3
  • 19
  • 38
  • `if (thisArray.indexOf('four') > -1)` should probably be `if (thisArray.indexOf('four') <= -1)`, or better yet, `if (thisArray.indexOf('four') < 0)`. – Tieson T. Aug 22 '18 at 21:23
1

You could use the bitwise NOT ~ operator and check the value of the returned index or -1 if not found.

~ is a bitwise not operator. It is perfect for use with indexOf(), because indexOf returns if found the index 0 ... n and if not -1:

value  ~value   boolean
-1  =>   0  =>  false
 0  =>  -1  =>  true
 1  =>  -2  =>  true
 2  =>  -3  =>  true
 and so on 

var theArray = new Array("one", "two", "three");

if (~theArray.indexOf("one")) {
    console.log("Found it!");
}
if (!~theArray.indexOf("four")) {
    console.log("Did not find it!");
}
Community
  • 1
  • 1
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

You'd think that might work, wouldn't you? But there are two gotchas here.

  1. .indexOf() returns the index of the first matching element it finds, or -1 if it doesn't find anything. And remember that JavaScript array's are zero-indexed, meaning the first array element has the index of zero. So if the match is the first element, then zero is the returned value, and like most languages, zero means false when you'd want it to be true. However this bring us to point #2.

  2. .indexOf() performs a comparison using strict equality, or in other words ===. The returned value won't be coerced like if you used true == 1. This is highly relevant here because if it didn't use strict equality, then any element it found (other than the first) would have an index of one or higher, and then your comparison would succeed. For example if (theArray.indexOf("two")) would work since the index of that element is 1. However, single indexOf() does a strict equality check, it fails. Which is why you need to explicitly compare the returned value of indexOf() to something, normally > -1.

j08691
  • 204,283
  • 31
  • 260
  • 272
1

Linear search. It is a 'language agnostic' approach to solving the problem of searching an unordered list. Yes, you can use array.includes(), which is a neat one-linear specific to JavaScript. But, it appears as through you are new to programming, at least with JavaScript, and before you take advantage of some of those fancy tools that make life easier, it's worth implementing them yourself so you truly understand what's going on under the hood and more importantly, why it works.

function contains(array, value) {


    // Loop through the entire array
    for (let i = 0; i < array.length; i++) {

        // Return true on first match
        if (array[i] === value)
            return true
    }

    // Return false on no match
    return false
}

// Make an array
let a = ['one', 'two', 'three']

// If it has an element, notify
if (contains(a, 'one'))
    alert('found it')