-4
    var JOURNAL = [
      {"events":["carrot","exercise","weekend"],"squirrel":false},
      {"events":["bread","pudding","brushed teeth","weekend","touched tree"],"squirrel":false}]

      function hasEvent(event, entry) {
          var x = entry.events.indexOf(event)

          console.log(x)
          //-1    

      return  x != -1;
    }
      hasEvent('pizza', JOURNAL[0])

For example: There is no 'pizza' in JOURNAL[0], so it returns -1 which causes x to return false.

I understand that if it doesn't found the event, it returns -1!

My question is why does it return -1? Is there any reason behind it?

Many Thanks

Edit: Is asking this sort of question bad? That's why i got downvoted? Sorry!

Blue Moon
  • 161
  • 1
  • 9
  • 3
    returns true if x != -1, false otherwise. – xlecoustillier Jan 08 '16 at 13:03
  • check the docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf I guess there is no particlular reason why it does so. But it is rather common to return -1 when something wasn't found – Patchee Jan 08 '16 at 13:05
  • 2
    Because the specs say so -- http://www.ecma-international.org/ecma-262/5.1/#sec-15.4.4.14 – Abhitalks Jan 08 '16 at 13:07
  • For not found indexes, indexOf returns -1 because all positive integers (including 0) could be indexes in an array. Rather than returning another type (e.g. a boolean false) it is simpler to return a negative integer. Another alternative is to return NaN but that would need testing for with isNaN. – BrettJephson Jan 08 '16 at 13:16
  • 2
    Possible duplicate of [What does this symbol mean in JavaScript?](http://stackoverflow.com/questions/9549780/what-does-this-symbol-mean-in-javascript) – Ian Jan 08 '16 at 15:17
  • 2
    @Sotra I think you're being downvoted because the title isn't good. Your title have "What Does x != -1 Mean?" but what you really want to know is "Why indexOf returns -1 when a element is not found". You might want to know what is the historical reason of -1 instead of null, undefined etc. Is it correct? – Marcio Junior Jan 08 '16 at 15:56
  • Exactly Marcio! Haha.. I need to work on my English. :( – Blue Moon Jan 09 '16 at 12:52

7 Answers7

2

-1 is used because it is the first, and presumably most memorable, number that couldn't be mistaken for an actual index in the array.

Any number zero or greater could be an index, and there's no reason to complicate things by having some random "magic number".

TZHX
  • 4,707
  • 14
  • 43
  • 52
0
return x != -1;

This returns the result of a conditional statement,
either true or false in case x does not equal -1

The F
  • 3,407
  • 1
  • 16
  • 26
0

That is how array indexOf works

var array = [2, 9, 9];
array.indexOf(2);     // 0
array.indexOf(7);     // -1

in your example

var events =  ["carrot","exercise","weekend"];
events .indexOf("carrot");     // 0
events .indexOf("csarrot");     // -1

your method hasEvent() returns x that is indexOf value over an array events

function hasEvent(event, entry) 
{
      var x = entry.events.indexOf(event); 
      return  x != -1; // if the event was found in entry.events array then it will return the index of the item otherwise -1
}
gurvinder372
  • 61,170
  • 7
  • 61
  • 75
0

indexOf returns -1 if event was not found. So, x!=-1 means event was found.

Axel
  • 13,204
  • 4
  • 44
  • 72
0

From Mozilla Developer:

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.

Considering this, the check x != -1 means that there is a check of whether event is found in entry.events array, in other words, "return whether the object is found in array or not".

Nick L.
  • 5,151
  • 4
  • 29
  • 46
0

When you search in array using indexOf, index of element is returned. in array, indexes start with 0. So first element will have index 0 and so on...

But if the element is not found, -1 is returned.

So when you say x != -1, it means that search element exists in array.

As per Array - MDN

JavaScript arrays are zero-indexed: the first element of an array is at index 0, and the last element is at the index equal to the value of the array's length property minus 1.

For more information about indexOf, check MDN.

Rajesh
  • 21,405
  • 5
  • 35
  • 66
0

My question is why does it return -1?

Because that's how the function array.indexOf is specified:

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. Source: Array.prototype.indexOf

Is there any reason behind it?

There is a logical reason to this: Arrays in JavaScript are 0-indexed. Therefore all values in the range (0, ..., array.length - 1) are valid index values, i.e. are potentially returned when the element is in the array. Returning the array.length to mean not found would be rather awkward as it would force the caller to always check against array.length to interpret the value.

Hence, -1 is the always obvious value to signify not found as it is never a valid index value.

What Does x != -1 mean?

It means that function hasEvent will return true if its argument event is found, false if not.

miraculixx
  • 8,614
  • 2
  • 32
  • 51