What does this -1 mean here? Why should we use it?

What does this -1 mean here? Why should we use it?


var john = ['John', 'Smith', 1990, 'designer', false];

if (john.indexOf('teacher') === -1) {
    console.log('John is NOT a teacher.');
}

Comments 1

  • john.indexOf('teacher') looks for the string 'teacher' in the array john
    If it finds it, it returns the index. In this case it doesn't find it(because john is not a teacher, or better said because the string 'teacher' is not present in the array john) and therefore returns -1.
    More info here:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf