0

I can't figure out if -1 is true or false in javascript, when I use indexOf.

let a =  'abc'.indexOf('abc');  
let b =  'def'.indexOf('abc');   

console.log(a);     // 0
console.log(b);     // -1
console.log(!a);    // true
console.log(!b);    // false

Why are the last two lines giving true/false?

From what I understand only == allows for type converting, since (=== is strict)

Is (!a) and (!b) using (==) internally somewhere?

roro
  • 193
  • 3
  • 16
  • It is true (well, "truthy"). The only "falsey" number is zero. You need to test if it `== -1` is true. – Blazemonger Jan 11 '16 at 16:10
  • 6
    It's neither, it's a number not a boolean, however it is *truthy* – adeneo Jan 11 '16 at 16:10
  • It all started in c where 0 was false and everything else was true. JavaScript tried to follow this, but it got messy quickly without strict type enforcement. – Hogan Jan 11 '16 at 16:27

4 Answers4

6

From MDN:

In JavaScript, a truthy value is a value that translates to true when evaluated in a Boolean context. All values are truthy unless they are defined as falsy (i.e., except for false, 0, "", null, undefined, and NaN). (emphasis mine)

This means -1 is considered "truthy". You shouldn't be checking for "truthiness" directly on the value returned from indexOf anyway. -1 has a specific meaning in that the element you are looking for does not exist in the array. So it would make more sense to explicitly test against -1 using ===. To anyone reading the code, the intent is also much clearer than coercing the return value of indexOf and making your decision based on that.

Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
  • Other than readability, is there anything else wrong with using (!) directly with indexOf? A case where value isn't found, will always return -1(truthy), which makes final result false. So why use the longer syntax of .indexOf('string') > -1 – roro Jan 11 '16 at 16:29
  • 1
    The longer syntax speaks to what you are actually doing (i.e. looking for the index of an element in an array). Valid indexes range from 0 to length - 1. Hence it is more intuitive rather than coercing the value to a boolean. Coercing and testing not very useful anyway, because other than `0`, every other index is "truthy", whereas the only value that you really care about is `-1` which *also* happens to be truthy. So you would have to do something like `arr.indexOf(i) && arr.indexOf(i) > -1` to see if you have a valid index. So why not just do `arr.indexOf(i) > -1` anyway? – Vivin Paliath Jan 11 '16 at 16:40
  • Just a minor edit to the comment above. The full expression would be `(arr.indexOf(i) && arr.indexOf(i) !== -1) || !arr.indexOf(i)`, which is more complicated than just checking `arr.indexOf(i) !== -1`. – Vivin Paliath Jan 11 '16 at 23:40
0

To check what is certain variables conversion to boolean, the best trick is to use boolean operator to it. For example ! (negation) is boolean operator that gives you negative (true for false and vice versa) of that value.

And this knowledge is in Javascript (and also PHP and Python) used like this:

var booleanValue = !!someVariable;

That is in your case:

!!(-1) //returns true

So yes, -1 will convert to true by implicit javascript conversion.

Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
0

In javascript, only "0" is false. When make a call with a preceeding "!", javascript will cast everything to boolean. Another interesting fact:

console.log(!!!a);    // false
console.log(!!!b);    // true

Thereby casting your "a" to a boolean with !! and getting the inverse value

Remi Smirra
  • 2,499
  • 1
  • 14
  • 15
0

-1 is a trule-like value. The only false-like value between numbers in JavaScript is 0. Other false-like values are '', NaN, undefined... I hope i didn't miss anything.

When using indexOf all you need to do is

if (someString.indexOf('value') >= -1) {
    //code if someString contains 'value'
}
Emil Nik
  • 29
  • 5